annotate Objects/dictobject.c @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents 1120041f2df4
children b68da8f2ed95
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
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
72 typedef struct {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
73 /* Cached hash code of me_key. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
74 Py_hash_t me_hash;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
75 PyObject *me_key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
76 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
77 } PyDictKeyEntry;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
78
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
79 typedef PyDictKeyEntry *(*dict_lookup_func)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
80 (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
81
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
82 struct _dictkeysobject {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
83 Py_ssize_t dk_refcnt;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
84 Py_ssize_t dk_size;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
85 dict_lookup_func dk_lookup;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
86 Py_ssize_t dk_usable;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
87 PyDictKeyEntry dk_entries[1];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
88 };
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
89
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
90
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
91 /*
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
92 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
93 slot (NULL key) in the table.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
94 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
95 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
96 */
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
97
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39657
diff changeset
98 /* Set a key error with the specified argument, wrapping it in a
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39657
diff changeset
99 * tuple automatically so that tuple keys are not unpacked as the
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39657
diff changeset
100 * exception arguments. */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39657
diff changeset
101 static void
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39657
diff changeset
102 set_key_error(PyObject *arg)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39657
diff changeset
103 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
104 PyObject *tup;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
105 tup = PyTuple_Pack(1, arg);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
106 if (!tup)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
107 return; /* caller will expect error to be set anyway */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
108 PyErr_SetObject(PyExc_KeyError, tup);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
109 Py_DECREF(tup);
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39657
diff changeset
110 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39657
diff changeset
111
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
112 #define PERTURB_SHIFT 5
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
113
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
114 /*
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
115 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
116 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
117 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
118 cases:
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
119
39657
ebad59c7233e Got test_mutants.py working. One set of changes was straightforward:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
120 >>> 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
121 [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
122 >>> 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
123 [-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
124 >>>
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
125
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
126 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
127 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
128 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
129 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
130 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
131
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
132 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
133 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
134 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
135 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
136 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
137 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
138
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
139 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
140 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
141 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
142 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
143 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
144 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
145
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
146 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
147 recurrence:
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
148
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
149 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
150
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
151 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
152 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
153 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
154 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
155 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
156 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
157 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
158 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
159
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
160 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
161
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
162 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
163 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
164 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
165 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
166 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
167 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
168
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
169 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
170 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
171 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
172
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
173 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
174 perturb >>= PERTURB_SHIFT;
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
175 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
176
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
177 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
178 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
179 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
180 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
181 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
182 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
183 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
184 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
185
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
186 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
187 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
188 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
189 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
190 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
191 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
192
39657
ebad59c7233e Got test_mutants.py working. One set of changes was straightforward:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
193 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
194 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
195 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
196 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
197 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
198 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
199 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
200 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
201 (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
202 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
203 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
204 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
205 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
206
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
207 */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
209 /* 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
210 * This could be any unique object,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
211 * 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
212 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
213 static PyObject _dummy_struct;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
214
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
215 #define dummy (&_dummy_struct)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
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
217 #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
218 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
219 _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
220 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
221 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
222 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36657
diff changeset
223 #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
224
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
225 /* forward declarations */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
226 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
227 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
228 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
229 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
230 static PyDictKeyEntry *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
231 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
232 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
233 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
234 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
235
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
236 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
237
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
238 /* 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
239 #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
240 #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
241 #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
242 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
243 static int numfree = 0;
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
244
73551
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
245 int
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
246 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
247 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
248 PyDictObject *op;
73551
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
249 int ret = numfree;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
250 while (numfree) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
251 op = free_list[--numfree];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
252 assert(PyDict_CheckExact(op));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
253 PyObject_GC_Del(op);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
254 }
73551
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
255 return ret;
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
256 }
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
257
77577
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
258 /* 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
259 void
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
260 _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
261 {
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
262 _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
263 "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
264 }
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
265
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
266
73551
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
267 void
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
268 PyDict_Fini(void)
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
269 {
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
270 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
271 }
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
272
76892
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
273 #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
274 #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
275
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
276 #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
277 #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
278 #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
279 #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
280 #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
281
77740
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
282 /* 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
283 * 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
284 * 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
285 * 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
286 * cost of total memory consumed.
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
287 *
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
288 * 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
289 * (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
290 *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
291 * 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
292 * 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
293 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
294
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
295 /* 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
296 * 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
297 * 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
298 * 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
299 #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
300
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
301 /* 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
302 * 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
303 * 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
304 * 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
305 * #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
306 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
307
77740
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
308 /* GROWTH_RATE. Growth rate upon hitting maximum load. Currently set to *2.
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
309 * Raising this to *4 doubles memory consumption depending on the size of
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
310 * the dictionary, but results in half the number of resizes, less effort to
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
311 * resize and better sparseness for some (but not all dict sizes).
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
312 * Setting to *4 eliminates every other resize step.
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
313 * GROWTH_RATE was set to *4 up to version 3.2.
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
314 */
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
315 #define GROWTH_RATE(x) ((x) * 2)
76485
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 #define ENSURE_ALLOWS_DELETIONS(d) \
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
318 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
319 (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
320 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
321
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
322 /* 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
323 * (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
324 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
325 static PyDictKeysObject empty_keys_struct = {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
326 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
327 1, /* dk_size */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
328 lookdict_split, /* dk_lookup */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
329 0, /* dk_usable (immutable) */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
330 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
331 { 0, 0, 0 } /* dk_entries (empty) */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
332 }
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
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
335 static PyObject *empty_values[1] = { NULL };
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
336
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
337 #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
338
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
339 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
340 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
341 PyDictKeysObject *dk;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
342 Py_ssize_t i;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
343 PyDictKeyEntry *ep0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
344
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
345 assert(size >= PyDict_MINSIZE_SPLIT);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
346 assert(IS_POWER_OF_2(size));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
347 dk = PyMem_MALLOC(sizeof(PyDictKeysObject) +
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
348 sizeof(PyDictKeyEntry) * (size-1));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
349 if (dk == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
350 PyErr_NoMemory();
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
351 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
352 }
76892
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
353 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
354 dk->dk_size = size;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
355 dk->dk_usable = USABLE_FRACTION(size);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
356 ep0 = &dk->dk_entries[0];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
357 /* 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
358 ep0->me_hash = 0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
359 for (i = 0; i < size; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
360 ep0[i].me_key = NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
361 ep0[i].me_value = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
362 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
363 dk->dk_lookup = lookdict_unicode_nodummy;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
364 return dk;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
365 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
366
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
367 static void
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
368 free_keys_object(PyDictKeysObject *keys)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
369 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
370 PyDictKeyEntry *entries = &keys->dk_entries[0];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
371 Py_ssize_t i, n;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
372 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
373 Py_XDECREF(entries[i].me_key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
374 Py_XDECREF(entries[i].me_value);
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 PyMem_FREE(keys);
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
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
379 #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
380
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
381 #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
382
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
383 /* 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
384 static PyObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
385 new_dict(PyDictKeysObject *keys, PyObject **values)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
386 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
387 PyDictObject *mp;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
388 if (numfree) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
389 mp = free_list[--numfree];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
390 assert (mp != NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
391 assert (Py_TYPE(mp) == &PyDict_Type);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
392 _Py_NewReference((PyObject *)mp);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
393 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
394 else {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
395 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
396 if (mp == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
397 DK_DECREF(keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
398 free_values(values);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
399 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
400 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
401 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
402 mp->ma_keys = keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
403 mp->ma_values = values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
404 mp->ma_used = 0;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
405 return (PyObject *)mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
407
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
408 /* 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
409 static PyObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
410 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
411 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
412 PyObject **values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
413 Py_ssize_t i, size;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
414
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
415 size = DK_SIZE(keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
416 values = new_values(size);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
417 if (values == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
418 DK_DECREF(keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
419 return PyErr_NoMemory();
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
420 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
421 for (i = 0; i < size; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
422 values[i] = NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
423 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
424 return new_dict(keys, values);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
425 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
426
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
427 PyObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
428 PyDict_New(void)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
429 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
430 return new_dict(new_keys_object(PyDict_MINSIZE_COMBINED), NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
431 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
432
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 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
435 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
436 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
437 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
438
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
439 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
440 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
441
10461
4abc5abaec9a Vladimir Marangozov contributed updated comments.
Guido van Rossum <guido@python.org>
parents: 9487
diff changeset
442 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
443
39657
ebad59c7233e Got test_mutants.py working. One set of changes was straightforward:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
444 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
445 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
446 Christian Tismer.
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
447
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
448 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
449 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
450 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
451 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
452 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
453 the <dummy> value.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
454 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
455 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
456 slot.
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
458 static PyDictKeyEntry *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
459 lookdict(PyDictObject *mp, PyObject *key,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
460 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
461 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
462 register size_t i;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
463 register size_t perturb;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
464 register PyDictKeyEntry *freeslot;
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 register size_t mask;
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
466 PyDictKeyEntry *ep0;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
467 register PyDictKeyEntry *ep;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
468 register int cmp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
469 PyObject *startkey;
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
470
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
471 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
472 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
473 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
474 i = (size_t)hash & mask;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
475 ep = &ep0[i];
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
476 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
477 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
478 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
479 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
480 if (ep->me_key == dummy)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
481 freeslot = ep;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
482 else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
483 if (ep->me_hash == hash) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
484 startkey = ep->me_key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
485 Py_INCREF(startkey);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
486 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
487 Py_DECREF(startkey);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
488 if (cmp < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
489 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
490 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
491 if (cmp > 0) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
492 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
493 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
494 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
495 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
496 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
497 /* 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
498 goto top;
61467
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 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
501 freeslot = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
502 }
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
503
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
504 /* 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
505 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
506 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
507 i = (i << 2) + i + perturb + 1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
508 ep = &ep0[i & mask];
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
509 if (ep->me_key == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
510 if (freeslot == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
511 *value_addr = &ep->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
512 return ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
513 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
514 *value_addr = &freeslot->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
515 return freeslot;
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 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
518 if (ep->me_key == key) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
519 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
520 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
521 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
522 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
523 startkey = ep->me_key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
524 Py_INCREF(startkey);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
525 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
526 Py_DECREF(startkey);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
527 if (cmp < 0) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
528 *value_addr = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
529 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
530 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
531 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
532 if (cmp > 0) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
533 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
534 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
535 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
536 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
537 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
538 /* 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
539 goto top;
61467
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 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
542 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
543 freeslot = ep;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
544 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
545 assert(0); /* NOT REACHED */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
546 return 0;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
548
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
549 /* Specialized version for string-only keys */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
550 static PyDictKeyEntry *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
551 lookdict_unicode(PyDictObject *mp, PyObject *key,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
552 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
553 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
554 register size_t i;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
555 register size_t perturb;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
556 register PyDictKeyEntry *freeslot;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
557 register 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
558 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
559 register PyDictKeyEntry *ep;
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
560
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
561 /* 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
562 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
563 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
564 that here. */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
565 if (!PyUnicode_CheckExact(key)) {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
566 mp->ma_keys->dk_lookup = lookdict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
567 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
568 }
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
569 i = (size_t)hash & mask;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
570 ep = &ep0[i];
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
571 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
572 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
573 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
574 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
575 if (ep->me_key == dummy)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
576 freeslot = ep;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
577 else {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
578 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
579 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
580 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
581 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
582 freeslot = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
583 }
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
584
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
585 /* 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
586 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
587 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
588 i = (i << 2) + i + perturb + 1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
589 ep = &ep0[i & mask];
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
590 if (ep->me_key == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
591 if (freeslot == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
592 *value_addr = &ep->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
593 return ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
594 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
595 *value_addr = &freeslot->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
596 return freeslot;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
597 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
598 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
599 if (ep->me_key == key
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
600 || (ep->me_hash == hash
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
601 && ep->me_key != dummy
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
602 && unicode_eq(ep->me_key, key))) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
603 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
604 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
605 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
606 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
607 freeslot = ep;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
608 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
609 assert(0); /* NOT REACHED */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
610 return 0;
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
611 }
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
612
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
613 /* 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
614 * will be present. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
615 static PyDictKeyEntry *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
616 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
617 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
618 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
619 register size_t i;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
620 register size_t perturb;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
621 register 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
622 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
623 register PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
624
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
625 /* 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
626 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
627 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
628 that here. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
629 if (!PyUnicode_CheckExact(key)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
630 mp->ma_keys->dk_lookup = lookdict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
631 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
632 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
633 i = (size_t)hash & mask;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
634 ep = &ep0[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
635 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
636 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
637 (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
638 *value_addr = &ep->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
639 return ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
640 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
641 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
642 i = (i << 2) + i + perturb + 1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
643 ep = &ep0[i & mask];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
644 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
645 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
646 (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
647 *value_addr = &ep->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
648 return ep;
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 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
651 assert(0); /* NOT REACHED */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
652 return 0;
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
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
655 /* Version of lookdict for split tables.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
656 * 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
657 * 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
658 * 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
659 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
660 static PyDictKeyEntry *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
661 lookdict_split(PyDictObject *mp, PyObject *key,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
662 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
663 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
664 register size_t i;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
665 register size_t perturb;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
666 register 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
667 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
668 register PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
669
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
670 if (!PyUnicode_CheckExact(key)) {
76503
34b6998efd2c fix instance dicts with str subclasses (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76489
diff changeset
671 ep = lookdict(mp, key, hash, value_addr);
34b6998efd2c fix instance dicts with str subclasses (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76489
diff changeset
672 /* 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
673 i = ep - ep0;
34b6998efd2c fix instance dicts with str subclasses (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76489
diff changeset
674 *value_addr = &mp->ma_values[i];
34b6998efd2c fix instance dicts with str subclasses (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76489
diff changeset
675 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
676 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
677 i = (size_t)hash & mask;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
678 ep = &ep0[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
679 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
680 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
681 (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
682 *value_addr = &mp->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
683 return ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
684 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
685 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
686 i = (i << 2) + i + perturb + 1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
687 ep = &ep0[i & mask];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
688 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
689 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
690 (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
691 *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
692 return ep;
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 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
695 assert(0); /* NOT REACHED */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
696 return 0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
697 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
698
60931
79128efe8a99 prevent the dict constructor from accepting non-string keyword args #8419
Benjamin Peterson <benjamin@python.org>
parents: 59224
diff changeset
699 int
79128efe8a99 prevent the dict constructor from accepting non-string keyword args #8419
Benjamin Peterson <benjamin@python.org>
parents: 59224
diff changeset
700 _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
701 {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
702 Py_ssize_t pos = 0;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
703 PyObject *key, *value;
66220
379029714e24 handle dict subclasses gracefully in PyArg_ValidateKeywordArguments
Benjamin Peterson <benjamin@python.org>
parents: 65753
diff changeset
704 assert(PyDict_Check(dict));
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
705 /* Shortcut */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
706 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
707 return 1;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
708 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
709 if (!PyUnicode_Check(key))
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
710 return 0;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
711 return 1;
60931
79128efe8a99 prevent the dict constructor from accepting non-string keyword args #8419
Benjamin Peterson <benjamin@python.org>
parents: 59224
diff changeset
712 }
79128efe8a99 prevent the dict constructor from accepting non-string keyword args #8419
Benjamin Peterson <benjamin@python.org>
parents: 59224
diff changeset
713
52291
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
714 #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
715 do { \
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
716 if (!_PyObject_GC_IS_TRACKED(mp)) { \
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
717 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
718 _PyObject_GC_MAY_BE_TRACKED(value)) { \
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
719 _PyObject_GC_TRACK(mp); \
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 } \
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
722 } while(0)
52291
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
723
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
724 void
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
725 _PyDict_MaybeUntrack(PyObject *op)
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
726 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
727 PyDictObject *mp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
728 PyObject *value;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
729 Py_ssize_t i, size;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
730
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
731 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
732 return;
52291
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
733
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
734 mp = (PyDictObject *) op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
735 size = DK_SIZE(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
736 if (_PyDict_HasSplitTable(mp)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
737 for (i = 0; i < size; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
738 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
739 continue;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
740 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
741 assert(!_PyObject_GC_MAY_BE_TRACKED(
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
742 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
743 return;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
744 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
745 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
746 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
747 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
748 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
749 for (i = 0; i < size; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
750 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
751 continue;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
752 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
753 _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
754 return;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
755 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
756 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
757 _PyObject_GC_UNTRACK(op);
52291
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
758 }
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
759
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
760 /* 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
761 * 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
762 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
763 static PyDictKeyEntry *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
764 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
765 PyObject ***value_addr)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
766 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
767 size_t i;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
768 size_t perturb;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
769 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
770 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
771 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
772
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
773 assert(key != NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
774 if (!PyUnicode_CheckExact(key))
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
775 mp->ma_keys->dk_lookup = lookdict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
776 i = hash & mask;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
777 ep = &ep0[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
778 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
779 i = (i << 2) + i + perturb + 1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
780 ep = &ep0[i & mask];
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
781 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
782 assert(ep->me_value == NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
783 if (mp->ma_values)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
784 *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
785 else
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
786 *value_addr = &ep->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
787 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
788 }
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
789
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
790 static int
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
791 insertion_resize(PyDictObject *mp)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
792 {
77740
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
793 return dictresize(mp, GROWTH_RATE(mp->ma_used));
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
794 }
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
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 /*
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 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
798 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
799 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
800 */
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
801 static int
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
802 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
803 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
804 PyObject *old_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
805 PyObject **value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
806 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
807 assert(key != dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
808
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
809 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
810 if (insertion_resize(mp) < 0)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
811 return -1;
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
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
814 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
815 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
816 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
817 }
76666
c5fd332e5857 change insertdict to not steal references (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76535
diff changeset
818 Py_INCREF(value);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
819 MAINTAIN_TRACKING(mp, key, value);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
820 old_value = *value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
821 if (old_value != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
822 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
823 *value_addr = value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
824 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
825 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
826 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
827 if (ep->me_key == NULL) {
76666
c5fd332e5857 change insertdict to not steal references (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76535
diff changeset
828 Py_INCREF(key);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
829 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
830 /* Need to resize. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
831 if (insertion_resize(mp) < 0) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
832 Py_DECREF(key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
833 Py_DECREF(value);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
834 return -1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
835 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
836 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
837 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
838 mp->ma_keys->dk_usable--;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
839 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
840 ep->me_key = key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
841 ep->me_hash = hash;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
842 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
843 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
844 if (ep->me_key == dummy) {
76666
c5fd332e5857 change insertdict to not steal references (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76535
diff changeset
845 Py_INCREF(key);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
846 ep->me_key = key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
847 ep->me_hash = hash;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
848 Py_DECREF(dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
849 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
850 assert(_PyDict_HasSplitTable(mp));
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 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
853 mp->ma_used++;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
854 *value_addr = value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
855 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
856 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
857 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
858 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
859 }
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
860
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
861 /*
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
862 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
863 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
864 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
865 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
866 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
867 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
868 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
869 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
870 */
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
871 static void
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
872 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
873 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
874 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
875 size_t i;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
876 size_t perturb;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
877 PyDictKeysObject *k = mp->ma_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
878 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
879 PyDictKeyEntry *ep0 = &k->dk_entries[0];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
880 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
881
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
882 assert(k->dk_lookup != NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
883 assert(value != NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
884 assert(key != NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
885 assert(key != dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
886 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
887 i = hash & mask;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
888 ep = &ep0[i];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
889 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
890 i = (i << 2) + i + perturb + 1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
891 ep = &ep0[i & mask];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
892 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
893 assert(ep->me_value == NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
894 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
895 ep->me_hash = hash;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
896 ep->me_value = value;
1256
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 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
900 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
901 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
902 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
903 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
904 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
905 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
906 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
907 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
908 */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
909 static int
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
910 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
911 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
912 Py_ssize_t newsize;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
913 PyDictKeysObject *oldkeys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
914 PyObject **oldvalues;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
915 Py_ssize_t i, oldsize;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
916
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
917 /* Find the smallest table size > minused. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
918 for (newsize = PyDict_MINSIZE_COMBINED;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
919 newsize <= minused && newsize > 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
920 newsize <<= 1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
921 ;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
922 if (newsize <= 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
923 PyErr_NoMemory();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
924 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
925 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
926 oldkeys = mp->ma_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
927 oldvalues = mp->ma_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
928 /* Allocate a new table. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
929 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
930 if (mp->ma_keys == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
931 mp->ma_keys = oldkeys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
932 return -1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
933 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
934 if (oldkeys->dk_lookup == lookdict)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
935 mp->ma_keys->dk_lookup = lookdict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
936 oldsize = DK_SIZE(oldkeys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
937 mp->ma_values = NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
938 /* 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
939 if (oldsize == 1) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
940 assert(oldkeys == Py_EMPTY_KEYS);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
941 DK_DECREF(oldkeys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
942 return 0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
943 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
944 /* 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
945 * 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
946 * 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
947 * 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
948 if (oldvalues != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
949 for (i = 0; i < oldsize; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
950 if (oldvalues[i] != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
951 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
952 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
953 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
954 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
955 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
956 /* Main loop */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
957 for (i = 0; i < oldsize; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
958 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
959 if (ep->me_value != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
960 assert(ep->me_key != dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
961 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
962 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
963 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
964 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
965 if (oldvalues != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
966 /* 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
967 for (i = 0; i < oldsize; i++)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
968 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
969 assert(oldvalues != empty_values);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
970 free_values(oldvalues);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
971 DK_DECREF(oldkeys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
972 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
973 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
974 assert(oldkeys->dk_lookup != lookdict_split);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
975 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
976 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
977 for (i = 0; i < oldsize; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
978 if (ep0[i].me_key == dummy)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
979 Py_DECREF(dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
980 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
981 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
982 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
983 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
984 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
985 return 0;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
986 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
987
76535
5d5b72a71898 distiguish between refusing to creating shared keys and error (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76530
diff changeset
988 /* 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
989 * 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
990 static PyDictKeysObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
991 make_keys_shared(PyObject *op)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
992 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
993 Py_ssize_t i;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
994 Py_ssize_t size;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
995 PyDictObject *mp = (PyDictObject *)op;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
996
76535
5d5b72a71898 distiguish between refusing to creating shared keys and error (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76530
diff changeset
997 if (!PyDict_CheckExact(op))
5d5b72a71898 distiguish between refusing to creating shared keys and error (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76530
diff changeset
998 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
999 if (!_PyDict_HasSplitTable(mp)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1000 PyDictKeyEntry *ep0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1001 PyObject **values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1002 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
1003 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
1004 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1005 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1006 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
1007 /* Remove dummy keys */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1008 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
1009 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1010 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1011 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
1012 /* Copy values into a new array */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1013 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
1014 size = DK_SIZE(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1015 values = new_values(size);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1016 if (values == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1017 PyErr_SetString(PyExc_MemoryError,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1018 "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
1019 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1020 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1021 for (i = 0; i < size; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1022 values[i] = ep0[i].me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1023 ep0[i].me_value = NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1024 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1025 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
1026 mp->ma_values = values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1027 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1028 DK_INCREF(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1029 return mp->ma_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1030 }
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1031
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1032 PyObject *
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1033 _PyDict_NewPresized(Py_ssize_t minused)
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1034 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1035 Py_ssize_t newsize;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1036 PyDictKeysObject *new_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1037 for (newsize = PyDict_MINSIZE_COMBINED;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1038 newsize <= minused && newsize > 0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1039 newsize <<= 1)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1040 ;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1041 new_keys = new_keys_object(newsize);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1042 if (new_keys == NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1043 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1044 return new_dict(new_keys, NULL);
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1045 }
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1046
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1047 /* 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
1048 * 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
1049 * 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
1050 * 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
1051 * (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
1052 * (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
1053 * 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
1054 * 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
1055 * 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
1056 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1057 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
1058 PyDict_GetItem(PyObject *op, PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1059 {
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
1060 Py_hash_t hash;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1061 PyDictObject *mp = (PyDictObject *)op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1062 PyDictKeyEntry *ep;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1063 PyThreadState *tstate;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1064 PyObject **value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1065
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1066 if (!PyDict_Check(op))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1067 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1068 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
1069 (hash = ((PyASCIIObject *) key)->hash) == -1)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1070 {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1071 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1072 if (hash == -1) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1073 PyErr_Clear();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1074 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1075 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1076 }
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1077
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1078 /* 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
1079 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
1080 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
1081 _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
1082 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
1083 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1084 &_PyThreadState_Current);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1085 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
1086 /* preserve the existing exception */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1087 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
1088 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
1089 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
1090 /* ignore errors */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1091 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
1092 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1093 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1094 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1095 else {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1096 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
1097 if (ep == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1098 PyErr_Clear();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1099 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1100 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1101 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1102 return *value_addr;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1103 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1104
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1105 /* 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
1106 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
1107 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
1108 */
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1109 PyObject *
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1110 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
1111 {
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
1112 Py_hash_t hash;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1113 PyDictObject*mp = (PyDictObject *)op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1114 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1115 PyObject **value_addr;
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1116
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1117 if (!PyDict_Check(op)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1118 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1119 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1120 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1121 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
1122 (hash = ((PyASCIIObject *) key)->hash) == -1)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1123 {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1124 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1125 if (hash == -1) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1126 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1127 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1128 }
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1129
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1130 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
1131 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1132 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1133 return *value_addr;
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1134 }
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1135
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1136 PyObject *
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1137 _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
1138 {
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1139 PyObject *kv;
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1140 kv = _PyUnicode_FromId(key); /* borrowed */
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1141 if (kv == NULL)
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1142 return NULL;
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1143 return PyDict_GetItemWithError(dp, kv);
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1144 }
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1145
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1146 /* Fast version of global value lookup.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1147 * Lookup in globals, then builtins.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1148 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1149 PyObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1150 _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
1151 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1152 PyObject *x;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1153 if (PyUnicode_CheckExact(key)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1154 PyObject **value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1155 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
1156 if (hash != -1) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1157 PyDictKeyEntry *e;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1158 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
1159 if (e == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1160 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1161 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1162 x = *value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1163 if (x != NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1164 return x;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1165 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
1166 if (e == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1167 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1168 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1169 x = *value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1170 return x;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1171 }
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
1172 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1173 x = PyDict_GetItemWithError((PyObject *)globals, key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1174 if (x != NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1175 return x;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1176 if (PyErr_Occurred())
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1177 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1178 return PyDict_GetItemWithError((PyObject *)builtins, key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1179 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1180
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
1181 /* 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
1182 * 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
1183 * 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
1184 * 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
1185 * 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
1186 */
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
1187 int
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1188 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
1189 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1190 PyDictObject *mp;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1191 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
1192 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
1193 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
1194 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
1195 }
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(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
1197 assert(value);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1198 mp = (PyDictObject *)op;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1199 if (!PyUnicode_CheckExact(key) ||
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1200 (hash = ((PyASCIIObject *) key)->hash) == -1)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1201 {
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
1202 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
1203 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
1204 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
1205 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1206
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1207 /* 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
1208 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
1209 }
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
1210
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1211 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
1212 PyDict_DelItem(PyObject *op, PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1213 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1214 PyDictObject *mp;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1215 Py_hash_t hash;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1216 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1217 PyObject *old_key, *old_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1218 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
1219
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1220 if (!PyDict_Check(op)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1221 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1222 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1223 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1224 assert(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1225 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
1226 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1227 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1228 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1229 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1230 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1231 mp = (PyDictObject *)op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1232 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
1233 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1234 return -1;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1235 if (*value_addr == NULL) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1236 set_key_error(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1237 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1238 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1239 old_value = *value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1240 *value_addr = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1241 mp->ma_used--;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1242 if (!_PyDict_HasSplitTable(mp)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1243 ENSURE_ALLOWS_DELETIONS(mp);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1244 old_key = ep->me_key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1245 Py_INCREF(dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1246 ep->me_key = dummy;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1247 Py_DECREF(old_key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1248 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1249 Py_DECREF(old_value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1250 return 0;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1251 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1252
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1253 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
1254 PyDict_Clear(PyObject *op)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1255 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1256 PyDictObject *mp;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1257 PyDictKeysObject *oldkeys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1258 PyObject **oldvalues;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1259 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
1260
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1261 if (!PyDict_Check(op))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1262 return;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1263 mp = ((PyDictObject *)op);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1264 oldkeys = mp->ma_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1265 oldvalues = mp->ma_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1266 if (oldvalues == empty_values)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1267 return;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1268 /* Empty the dict... */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1269 DK_INCREF(Py_EMPTY_KEYS);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1270 mp->ma_keys = Py_EMPTY_KEYS;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1271 mp->ma_values = empty_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1272 mp->ma_used = 0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1273 /* ...then clear the keys and values */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1274 if (oldvalues != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1275 n = DK_SIZE(oldkeys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1276 for (i = 0; i < n; i++)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1277 Py_CLEAR(oldvalues[i]);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1278 free_values(oldvalues);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1279 DK_DECREF(oldkeys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1280 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1281 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1282 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
1283 DK_DECREF(oldkeys);
76485
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
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1287 /* 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
1288 * 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
1289 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1290 Py_LOCAL_INLINE(Py_ssize_t)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1291 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
1292 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1293 Py_ssize_t mask, offset;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1294 PyDictObject *mp;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1295 PyObject **value_ptr;
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
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1298 if (!PyDict_Check(op))
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1299 return -1;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1300 mp = (PyDictObject *)op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1301 if (i < 0)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1302 return -1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1303 if (mp->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1304 value_ptr = &mp->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1305 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1306 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1307 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1308 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
1309 offset = sizeof(PyDictKeyEntry);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1310 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1311 mask = DK_MASK(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1312 while (i <= mask && *value_ptr == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1313 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
1314 i++;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1315 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1316 if (i > mask)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1317 return -1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1318 if (pvalue)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1319 *pvalue = *value_ptr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1320 return i;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1321 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1322
27901
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
1323 /*
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
1324 * 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
1325 *
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1326 * 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
1327 * PyObject *key, *value;
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
1328 * 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
1329 * 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
1330 * 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
1331 * }
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
1332 *
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
1333 * 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
1334 * 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
1335 * 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
1336 * 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
1337 */
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1338 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1339 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
1340 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1341 PyDictObject *mp;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1342 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
1343 if (i < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1344 return 0;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1345 mp = (PyDictObject *)op;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1346 *ppos = i+1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1347 if (pkey)
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1348 *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
1349 return 1;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1350 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1351
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1352 /* 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
1353 * to the key and value.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1354 */
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
1355 int
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1356 _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
1357 PyObject **pvalue, Py_hash_t *phash)
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
1358 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1359 PyDictObject *mp;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1360 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
1361 if (i < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1362 return 0;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1363 mp = (PyDictObject *)op;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1364 *ppos = i+1;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1365 *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
1366 if (pkey)
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1367 *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
1368 return 1;
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
1369 }
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
1370
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1371 /* Methods */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1372
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1373 static void
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1374 dict_dealloc(PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1375 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1376 PyObject **values = mp->ma_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1377 PyDictKeysObject *keys = mp->ma_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1378 Py_ssize_t i, n;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1379 PyObject_GC_UnTrack(mp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1380 Py_TRASHCAN_SAFE_BEGIN(mp)
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1381 if (values != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1382 if (values != empty_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1383 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
1384 Py_XDECREF(values[i]);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1385 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1386 free_values(values);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1387 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1388 DK_DECREF(keys);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1389 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1390 else {
76892
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
1391 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
1392 DK_DECREF(keys);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1393 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1394 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
1395 free_list[numfree++] = mp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1396 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1397 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
1398 Py_TRASHCAN_SAFE_END(mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1399 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1400
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1401
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1402 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
1403 dict_repr(PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1404 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1405 Py_ssize_t i;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1406 PyObject *s, *temp, *colon = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1407 PyObject *pieces = NULL, *result = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1408 PyObject *key, *value;
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
1409
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1410 i = Py_ReprEnter((PyObject *)mp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1411 if (i != 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1412 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1413 }
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1414
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1415 if (mp->ma_used == 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1416 result = PyUnicode_FromString("{}");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1417 goto Done;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1418 }
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
1419
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1420 pieces = PyList_New(0);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1421 if (pieces == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1422 goto Done;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1423
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1424 colon = PyUnicode_FromString(": ");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1425 if (colon == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1426 goto Done;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1427
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1428 /* 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
1429 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
1430 i = 0;
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)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1432 int status;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1433 /* 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
1434 Py_INCREF(key);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1435 Py_INCREF(value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1436 s = PyObject_Repr(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1437 PyUnicode_Append(&s, colon);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1438 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1439 Py_DECREF(key);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1440 Py_DECREF(value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1441 if (s == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1442 goto Done;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1443 status = PyList_Append(pieces, s);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1444 Py_DECREF(s); /* append created a new ref */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1445 if (status < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1446 goto Done;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1447 }
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1448
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1449 /* Add "{}" decorations to the first and last items. */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1450 assert(PyList_GET_SIZE(pieces) > 0);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1451 s = PyUnicode_FromString("{");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1452 if (s == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1453 goto Done;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1454 temp = PyList_GET_ITEM(pieces, 0);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1455 PyUnicode_AppendAndDel(&s, temp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1456 PyList_SET_ITEM(pieces, 0, s);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1457 if (s == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1458 goto Done;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1459
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1460 s = PyUnicode_FromString("}");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1461 if (s == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1462 goto Done;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1463 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1464 PyUnicode_AppendAndDel(&temp, s);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1465 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1466 if (temp == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1467 goto Done;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1468
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1469 /* Paste them all together with ", " between. */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1470 s = PyUnicode_FromString(", ");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1471 if (s == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1472 goto Done;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1473 result = PyUnicode_Join(s, pieces);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1474 Py_DECREF(s);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1475
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1476 Done:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1477 Py_XDECREF(pieces);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1478 Py_XDECREF(colon);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1479 Py_ReprLeave((PyObject *)mp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1480 return result;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1481 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1482
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1483 static Py_ssize_t
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
1484 dict_length(PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1485 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1486 return mp->ma_used;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1487 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1488
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1489 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
1490 dict_subscript(PyDictObject *mp, register PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1491 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1492 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
1493 Py_hash_t hash;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1494 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1495 PyObject **value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1496
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1497 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
1498 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1499 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1500 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1501 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1502 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1503 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
1504 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1505 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1506 v = *value_addr;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1507 if (v == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1508 if (!PyDict_CheckExact(mp)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1509 /* 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
1510 PyObject *missing, *res;
74578
7d96679c5d46 use the static identifier api for looking up special methods
Benjamin Peterson <benjamin@python.org>
parents: 74045
diff changeset
1511 _Py_IDENTIFIER(__missing__);
7d96679c5d46 use the static identifier api for looking up special methods
Benjamin Peterson <benjamin@python.org>
parents: 74045
diff changeset
1512 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
1513 if (missing != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1514 res = PyObject_CallFunctionObjArgs(missing,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1515 key, NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1516 Py_DECREF(missing);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1517 return res;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1518 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1519 else if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1520 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1521 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1522 set_key_error(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1523 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1524 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1525 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1526 Py_INCREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1527 return v;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1528 }
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 static int
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
1531 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
1532 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1533 if (w == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1534 return PyDict_DelItem((PyObject *)mp, v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1535 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1536 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
1537 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1538
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1539 static PyMappingMethods dict_as_mapping = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1540 (lenfunc)dict_length, /*mp_length*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1541 (binaryfunc)dict_subscript, /*mp_subscript*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1542 (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
1543 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1544
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1545 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
1546 dict_keys(register PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1547 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1548 register PyObject *v;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1549 register Py_ssize_t i, j;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1550 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1551 Py_ssize_t size, n, offset;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1552 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
1553
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1554 again:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1555 n = mp->ma_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1556 v = PyList_New(n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1557 if (v == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1558 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1559 if (n != mp->ma_used) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1560 /* 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
1561 * 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
1562 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1563 Py_DECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1564 goto again;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1565 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1566 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
1567 size = DK_SIZE(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1568 if (mp->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1569 value_ptr = mp->ma_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1570 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1571 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1572 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1573 value_ptr = &ep[0].me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1574 offset = sizeof(PyDictKeyEntry);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1575 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1576 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
1577 if (*value_ptr != NULL) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1578 PyObject *key = ep[i].me_key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1579 Py_INCREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1580 PyList_SET_ITEM(v, j, key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1581 j++;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1582 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1583 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
1584 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1585 assert(j == n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1586 return v;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1587 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1588
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1589 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
1590 dict_values(register PyDictObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1591 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1592 register PyObject *v;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1593 register Py_ssize_t i, j;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1594 Py_ssize_t size, n, offset;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1595 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
1596
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1597 again:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1598 n = mp->ma_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1599 v = PyList_New(n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1600 if (v == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1601 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1602 if (n != mp->ma_used) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1603 /* 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
1604 * 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
1605 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1606 Py_DECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1607 goto again;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1608 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1609 size = DK_SIZE(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1610 if (mp->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1611 value_ptr = mp->ma_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1612 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1613 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1614 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1615 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
1616 offset = sizeof(PyDictKeyEntry);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1617 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1618 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
1619 PyObject *value = *value_ptr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1620 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
1621 if (value != NULL) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1622 Py_INCREF(value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1623 PyList_SET_ITEM(v, j, value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1624 j++;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1625 }
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 assert(j == n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1628 return v;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1629 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1630
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1631 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
1632 dict_items(register PyDictObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1633 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1634 register PyObject *v;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1635 register 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
1636 Py_ssize_t size, offset;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1637 PyObject *item, *key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1638 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1639 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
1640
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1641 /* 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
1642 * 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
1643 * could resize the dict. :-(
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1644 */
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1645 again:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1646 n = mp->ma_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1647 v = PyList_New(n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1648 if (v == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1649 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1650 for (i = 0; i < n; i++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1651 item = PyTuple_New(2);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1652 if (item == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1653 Py_DECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1654 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1655 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1656 PyList_SET_ITEM(v, i, item);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1657 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1658 if (n != mp->ma_used) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1659 /* 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
1660 * 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
1661 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1662 Py_DECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1663 goto again;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1664 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1665 /* 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
1666 ep = mp->ma_keys->dk_entries;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1667 size = DK_SIZE(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1668 if (mp->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1669 value_ptr = mp->ma_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1670 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1671 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1672 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1673 value_ptr = &ep[0].me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1674 offset = sizeof(PyDictKeyEntry);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1675 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1676 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
1677 PyObject *value = *value_ptr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1678 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
1679 if (value != NULL) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1680 key = ep[i].me_key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1681 item = PyList_GET_ITEM(v, j);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1682 Py_INCREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1683 PyTuple_SET_ITEM(item, 0, key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1684 Py_INCREF(value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1685 PyTuple_SET_ITEM(item, 1, value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1686 j++;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1687 }
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 assert(j == n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1690 return v;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1691 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1692
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1693 static PyObject *
26583
8991f78817c7 SF 548651: Fix the METH_CLASS implementation.
Tim Peters <tim.peters@gmail.com>
parents: 26562
diff changeset
1694 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
1695 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1696 PyObject *seq;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1697 PyObject *value = Py_None;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1698 PyObject *it; /* iter(seq) */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1699 PyObject *key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1700 PyObject *d;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1701 int status;
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1702
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1703 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
1704 return NULL;
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1705
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1706 d = PyObject_CallObject(cls, NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1707 if (d == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1708 return NULL;
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1709
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1710 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1711 PyDictObject *mp = (PyDictObject *)d;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1712 PyObject *oldvalue;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1713 Py_ssize_t pos = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1714 PyObject *key;
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
1715 Py_hash_t hash;
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1716
73103
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1717 if (dictresize(mp, Py_SIZE(seq))) {
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1718 Py_DECREF(d);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1719 return NULL;
73103
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1720 }
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1721
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1722 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
73103
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1723 if (insertdict(mp, key, hash, value)) {
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1724 Py_DECREF(d);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1725 return NULL;
73103
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1726 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1727 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1728 return d;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1729 }
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1730
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1731 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1732 PyDictObject *mp = (PyDictObject *)d;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1733 Py_ssize_t pos = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1734 PyObject *key;
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
1735 Py_hash_t hash;
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40962
diff changeset
1736
73103
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1737 if (dictresize(mp, PySet_GET_SIZE(seq))) {
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1738 Py_DECREF(d);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1739 return NULL;
73103
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1740 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40962
diff changeset
1741
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1742 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
73103
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1743 if (insertdict(mp, key, hash, value)) {
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1744 Py_DECREF(d);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1745 return NULL;
73103
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1746 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1747 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1748 return d;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1749 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40962
diff changeset
1750
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1751 it = PyObject_GetIter(seq);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1752 if (it == NULL){
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1753 Py_DECREF(d);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1754 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1755 }
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1756
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1757 if (PyDict_CheckExact(d)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1758 while ((key = PyIter_Next(it)) != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1759 status = PyDict_SetItem(d, key, value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1760 Py_DECREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1761 if (status < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1762 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1763 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1764 } else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1765 while ((key = PyIter_Next(it)) != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1766 status = PyObject_SetItem(d, key, value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1767 Py_DECREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1768 if (status < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1769 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1770 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1771 }
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1772
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1773 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1774 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1775 Py_DECREF(it);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1776 return d;
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1777
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1778 Fail:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1779 Py_DECREF(it);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1780 Py_DECREF(d);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1781 return NULL;
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1782 }
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1783
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1784 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
1785 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
1786 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1787 PyObject *arg = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1788 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
1789
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1790 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
1791 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
1792
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1793 else if (arg != NULL) {
72926
db4aa878cea2 Port SetAttrString/HasAttrString to SetAttrId/GetAttrId.
Martin v. Löwis <martin@v.loewis.de>
parents: 72925
diff changeset
1794 _Py_IDENTIFIER(keys);
db4aa878cea2 Port SetAttrString/HasAttrString to SetAttrId/GetAttrId.
Martin v. Löwis <martin@v.loewis.de>
parents: 72925
diff changeset
1795 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
1796 result = PyDict_Merge(self, arg, 1);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1797 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1798 result = PyDict_MergeFromSeq2(self, arg, 1);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1799 }
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1800 if (result == 0 && kwds != NULL) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1801 if (PyArg_ValidateKeywordArguments(kwds))
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1802 result = PyDict_Merge(self, kwds, 1);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1803 else
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1804 result = -1;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1805 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1806 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
1807 }
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1808
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1809 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
1810 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
1811 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1812 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
1813 Py_RETURN_NONE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1814 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1815 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1816
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1817 /* Update unconditionally replaces existing items.
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1818 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
1819 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
1820
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1821 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
1822
21348
12310d643d8d SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents: 21155
diff changeset
1823 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
1824 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
1825 */
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1826
21348
12310d643d8d SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents: 21155
diff changeset
1827 int
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1828 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
1829 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1830 PyObject *it; /* iter(seq2) */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1831 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
1832 PyObject *item; /* seq2[i] */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1833 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
1834
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1835 assert(d != NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1836 assert(PyDict_Check(d));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1837 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
1838
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1839 it = PyObject_GetIter(seq2);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1840 if (it == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1841 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
1842
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1843 for (i = 0; ; ++i) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1844 PyObject *key, *value;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1845 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
1846
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1847 fast = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1848 item = PyIter_Next(it);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1849 if (item == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1850 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1851 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1852 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1853 }
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1854
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1855 /* 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
1856 fast = PySequence_Fast(item, "");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1857 if (fast == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1858 if (PyErr_ExceptionMatches(PyExc_TypeError))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1859 PyErr_Format(PyExc_TypeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1860 "cannot convert dictionary update "
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1861 "sequence element #%zd to a sequence",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1862 i);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1863 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1864 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1865 n = PySequence_Fast_GET_SIZE(fast);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1866 if (n != 2) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1867 PyErr_Format(PyExc_ValueError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1868 "dictionary update sequence element #%zd "
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1869 "has length %zd; 2 is required",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1870 i, n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1871 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1872 }
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1873
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1874 /* 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
1875 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
1876 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
1877 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
1878 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
1879 if (status < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1880 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1881 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1882 Py_DECREF(fast);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1883 Py_DECREF(item);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1884 }
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1885
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1886 i = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1887 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
1888 Fail:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1889 Py_XDECREF(item);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1890 Py_XDECREF(fast);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1891 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
1892 Return:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1893 Py_DECREF(it);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1894 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
1895 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1896
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1897 int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1898 PyDict_Update(PyObject *a, PyObject *b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1899 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1900 return PyDict_Merge(a, b, 1);
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1901 }
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1902
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1903 int
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1904 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
1905 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1906 register PyDictObject *mp, *other;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1907 register Py_ssize_t i, n;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1908 PyDictKeyEntry *entry;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1909
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1910 /* 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
1911 * 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
1912 * 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
1913 * 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
1914 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1915 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
1916 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1917 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1918 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1919 mp = (PyDictObject*)a;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1920 if (PyDict_Check(b)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1921 other = (PyDictObject*)b;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1922 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
1923 /* 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
1924 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1925 if (mp->ma_used == 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1926 /* 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
1927 * 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
1928 * skips the unnecessary test.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1929 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1930 override = 1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1931 /* 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
1932 * 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
1933 * 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
1934 */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1935 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
1936 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
1937 return -1;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1938 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
1939 PyObject *value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1940 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
1941 if (other->ma_values)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1942 value = other->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1943 else
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1944 value = entry->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1945
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1946 if (value != NULL &&
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1947 (override ||
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1948 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
1949 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
1950 entry->me_hash,
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1951 value) != 0)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1952 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1953 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1954 }
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 else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1957 /* Do it the generic, slower way */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1958 PyObject *keys = PyMapping_Keys(b);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1959 PyObject *iter;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1960 PyObject *key, *value;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1961 int status;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1962
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1963 if (keys == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1964 /* 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
1965 * 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
1966 * 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
1967 * 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
1968 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1969 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1970
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1971 iter = PyObject_GetIter(keys);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1972 Py_DECREF(keys);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1973 if (iter == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1974 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1975
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1976 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
1977 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
1978 Py_DECREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1979 continue;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1980 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1981 value = PyObject_GetItem(b, key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1982 if (value == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1983 Py_DECREF(iter);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1984 Py_DECREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1985 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1986 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1987 status = PyDict_SetItem(a, key, value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1988 Py_DECREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1989 Py_DECREF(value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1990 if (status < 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1991 Py_DECREF(iter);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1992 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1993 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1994 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1995 Py_DECREF(iter);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1996 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1997 /* Iterator completed, via error */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1998 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1999 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2000 return 0;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
2001 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
2002
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
2003 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2004 dict_copy(register PyDictObject *mp)
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
2005 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2006 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
2007 }
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
2008
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 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
2010 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
2011 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2012 PyObject *copy;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2013 PyDictObject *mp;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2014 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
2015
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2016 if (o == NULL || !PyDict_Check(o)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2017 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2018 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2019 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2020 mp = (PyDictObject *)o;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2021 if (_PyDict_HasSplitTable(mp)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2022 PyDictObject *split_copy;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2023 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
2024 if (newvalues == NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2025 return PyErr_NoMemory();
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2026 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
2027 if (split_copy == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2028 free_values(newvalues);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2029 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2030 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2031 split_copy->ma_values = newvalues;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2032 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
2033 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
2034 DK_INCREF(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2035 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
2036 PyObject *value = mp->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2037 Py_XINCREF(value);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2038 split_copy->ma_values[i] = value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2039 }
76517
507a6703d6a3 fix dict gc tracking (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76503
diff changeset
2040 if (_PyObject_GC_IS_TRACKED(mp))
507a6703d6a3 fix dict gc tracking (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76503
diff changeset
2041 _PyObject_GC_TRACK(split_copy);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2042 return (PyObject *)split_copy;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2043 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2044 copy = PyDict_New();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2045 if (copy == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2046 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2047 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
2048 return copy;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2049 Py_DECREF(copy);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2050 return NULL;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
2051 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
2052
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2053 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
2054 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
2055 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2056 if (mp == NULL || !PyDict_Check(mp)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2057 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2058 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2059 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2060 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
2061 }
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
2062
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2063 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
2064 PyDict_Keys(PyObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2065 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2066 if (mp == NULL || !PyDict_Check(mp)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2067 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2068 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2069 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2070 return dict_keys((PyDictObject *)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2071 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2072
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2073 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
2074 PyDict_Values(PyObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
2075 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2076 if (mp == NULL || !PyDict_Check(mp)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2077 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2078 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2079 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2080 return dict_values((PyDictObject *)mp);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
2081 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
2082
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2083 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
2084 PyDict_Items(PyObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
2085 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2086 if (mp == NULL || !PyDict_Check(mp)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2087 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2088 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2089 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2090 return dict_items((PyDictObject *)mp);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
2091 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
2092
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2093 /* 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
2094 * 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
2095 * Uses only Py_EQ comparison.
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2096 */
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2097 static int
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2098 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
2099 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2100 Py_ssize_t i;
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 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
2103 /* 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
2104 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2105 /* 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
2106 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
2107 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
2108 PyObject *aval;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2109 if (a->ma_values)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2110 aval = a->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2111 else
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2112 aval = ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2113 if (aval != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2114 int cmp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2115 PyObject *bval;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2116 PyObject *key = ep->me_key;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2117 /* 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
2118 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
2119 Py_INCREF(aval);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2120 /* ditto for key */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2121 Py_INCREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2122 bval = PyDict_GetItemWithError((PyObject *)b, key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2123 Py_DECREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2124 if (bval == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2125 Py_DECREF(aval);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2126 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2127 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2128 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2129 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2130 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
2131 Py_DECREF(aval);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2132 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
2133 return cmp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2134 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2135 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2136 return 1;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2137 }
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2138
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2139 static PyObject *
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2140 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
2141 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2142 int cmp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2143 PyObject *res;
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2144
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2145 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
2146 res = Py_NotImplemented;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2147 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2148 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
2149 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
2150 if (cmp < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2151 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2152 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
2153 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2154 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2155 res = Py_NotImplemented;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2156 Py_INCREF(res);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2157 return res;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2158 }
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2159
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2160 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2161 dict_contains(register PyDictObject *mp, PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2162 {
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
2163 Py_hash_t hash;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2164 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2165 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
2166
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2167 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
2168 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2169 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2170 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2171 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2172 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2173 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
2174 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2175 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2176 return PyBool_FromLong(*value_addr != NULL);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2177 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2178
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2179 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2180 dict_get(register PyDictObject *mp, PyObject *args)
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
2181 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2182 PyObject *key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2183 PyObject *failobj = Py_None;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2184 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
2185 Py_hash_t hash;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2186 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2187 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
2188
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2189 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
2190 return NULL;
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
2191
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2192 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
2193 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2194 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2195 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2196 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2197 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2198 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
2199 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2200 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2201 val = *value_addr;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2202 if (val == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2203 val = failobj;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2204 Py_INCREF(val);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2205 return val;
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
2206 }
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
2207
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
2208 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2209 dict_setdefault(register PyDictObject *mp, PyObject *args)
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
2210 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2211 PyObject *key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2212 PyObject *failobj = Py_None;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2213 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
2214 Py_hash_t hash;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2215 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2216 PyObject **value_addr;
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
2217
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2218 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2219 return NULL;
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
2220
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2221 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
2222 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2223 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2224 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2225 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2226 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2227 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
2228 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2229 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2230 val = *value_addr;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2231 if (val == NULL) {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2232 Py_INCREF(failobj);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2233 Py_INCREF(key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2234 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
2235 /* Need to resize. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2236 if (insertion_resize(mp) < 0)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2237 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2238 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
2239 }
76517
507a6703d6a3 fix dict gc tracking (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76503
diff changeset
2240 MAINTAIN_TRACKING(mp, key, failobj);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2241 ep->me_key = key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2242 ep->me_hash = hash;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2243 *value_addr = failobj;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2244 val = failobj;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2245 mp->ma_keys->dk_usable--;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2246 mp->ma_used++;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2247 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2248 Py_INCREF(val);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2249 return val;
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
2250 }
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
2251
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
2252
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
2253 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2254 dict_clear(register PyDictObject *mp)
5209
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
2255 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2256 PyDict_Clear((PyObject *)mp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2257 Py_RETURN_NONE;
5209
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
2258 }
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
2259
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2260 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2261 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
2262 {
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
2263 Py_hash_t hash;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2264 PyObject *old_value, *old_key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2265 PyObject *key, *deflt = NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2266 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2267 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
2268
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2269 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
2270 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2271 if (mp->ma_used == 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2272 if (deflt) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2273 Py_INCREF(deflt);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2274 return deflt;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2275 }
65754
480e45828d4b Issue 10221: Improve error message for dict.pop().
Raymond Hettinger <python@rcn.com>
parents: 61467
diff changeset
2276 set_key_error(key);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2277 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2278 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2279 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
2280 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2281 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2282 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2283 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2284 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2285 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
2286 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2287 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2288 old_value = *value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2289 if (old_value == NULL) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2290 if (deflt) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2291 Py_INCREF(deflt);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2292 return deflt;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2293 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2294 set_key_error(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2295 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2296 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2297 *value_addr = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2298 mp->ma_used--;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2299 if (!_PyDict_HasSplitTable(mp)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2300 ENSURE_ALLOWS_DELETIONS(mp);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2301 old_key = ep->me_key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2302 Py_INCREF(dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2303 ep->me_key = dummy;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2304 Py_DECREF(old_key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2305 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2306 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
2307 }
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2308
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2309 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2310 dict_popitem(PyDictObject *mp)
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2311 {
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
2312 Py_hash_t i = 0;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2313 PyDictKeyEntry *ep;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2314 PyObject *res;
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2315
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2316
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2317 /* 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
2318 * 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
2319 * 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
2320 * 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
2321 * 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
2322 * 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
2323 * 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
2324 * inefficiency -- possible, but unlikely in practice.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2325 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2326 res = PyTuple_New(2);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2327 if (res == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2328 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2329 if (mp->ma_used == 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2330 Py_DECREF(res);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2331 PyErr_SetString(PyExc_KeyError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2332 "popitem(): dictionary is empty");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2333 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2334 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2335 /* Convert split table to combined table */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2336 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
2337 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
2338 Py_DECREF(res);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2339 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2340 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2341 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2342 ENSURE_ALLOWS_DELETIONS(mp);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2343 /* 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
2344 * 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
2345 * 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
2346 * 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
2347 * 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
2348 */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2349 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
2350 if (ep->me_value == NULL) {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2351 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
2352 i = ep->me_hash;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2353 /* 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
2354 * 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
2355 * 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
2356 * 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
2357 */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2358 if (i > mask || i < 1)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2359 i = 1; /* skip slot 0 */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2360 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
2361 i++;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2362 if (i > mask)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2363 i = 1;
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 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2366 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
2367 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
2368 Py_INCREF(dummy);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2369 ep->me_key = dummy;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2370 ep->me_value = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2371 mp->ma_used--;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2372 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
2373 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
2374 return res;
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2375 }
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2376
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2377 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2378 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
2379 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2380 Py_ssize_t i, n;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2381 PyDictObject *mp = (PyDictObject *)op;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2382 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
2383 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
2384 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
2385 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
2386 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
2387 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2388 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2389 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2390 if (mp->ma_values != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2391 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
2392 Py_VISIT(mp->ma_values[i]);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2393 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2394 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2395 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2396 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
2397 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
2398 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2399 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2400 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2401 return 0;
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2402 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2403
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2404 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2405 dict_tp_clear(PyObject *op)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2406 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2407 PyDict_Clear(op);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2408 return 0;
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2409 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2410
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2411 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
2412
47466
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2413 static PyObject *
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2414 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
2415 {
76530
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2416 Py_ssize_t size, res;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2417
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2418 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
2419 res = sizeof(PyDictObject);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2420 if (mp->ma_values)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2421 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
2422 /* 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
2423 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
2424 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
2425 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2426 return PyLong_FromSsize_t(res);
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2427 }
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2428
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2429 Py_ssize_t
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2430 _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
2431 {
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2432 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
2433 }
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2434
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2435 PyDoc_STRVAR(contains__doc__,
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2436 "D.__contains__(k) -> True if D has a key k, else False");
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2437
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2438 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
2439
47466
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2440 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
2441 "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
2442
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2443 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
2444 "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
2445
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2446 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
2447 "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
2448
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2449 PyDoc_STRVAR(pop__doc__,
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48680
diff changeset
2450 "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
2451 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
2452
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2453 PyDoc_STRVAR(popitem__doc__,
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2454 "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
2455 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
2456
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2457 PyDoc_STRVAR(update__doc__,
74044
393906d020fe Small clarification in docstring of dict.update(): the positional argument is not required.
Georg Brandl <georg@python.org>
parents: 73103
diff changeset
2458 "D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n"
393906d020fe Small clarification in docstring of dict.update(): the positional argument is not required.
Georg Brandl <georg@python.org>
parents: 73103
diff changeset
2459 "If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\
393906d020fe Small clarification in docstring of dict.update(): the positional argument is not required.
Georg Brandl <georg@python.org>
parents: 73103
diff changeset
2460 If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48680
diff changeset
2461 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
2462
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
2463 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
2464 "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
2465 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
2466
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2467 PyDoc_STRVAR(clear__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2468 "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
2469
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2470 PyDoc_STRVAR(copy__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2471 "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
2472
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2473 /* Forward */
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2474 static PyObject *dictkeys_new(PyObject *);
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2475 static PyObject *dictitems_new(PyObject *);
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2476 static PyObject *dictvalues_new(PyObject *);
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2477
42381
9e84b4868984 Fix the docstrings for keys(), items(), values() (especially the latter).
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
2478 PyDoc_STRVAR(keys__doc__,
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2479 "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
2480 PyDoc_STRVAR(items__doc__,
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2481 "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
2482 PyDoc_STRVAR(values__doc__,
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2483 "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
2484
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2485 static PyMethodDef mapp_methods[] = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2486 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2487 contains__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2488 {"__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
2489 getitem__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2490 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2491 sizeof__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2492 {"get", (PyCFunction)dict_get, METH_VARARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2493 get__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2494 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2495 setdefault_doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2496 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2497 pop__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2498 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2499 popitem__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2500 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2501 keys__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2502 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2503 items__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2504 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2505 values__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2506 {"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
2507 update__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2508 {"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
2509 fromkeys__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2510 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2511 clear__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2512 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2513 copy__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2514 {NULL, NULL} /* sentinel */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2515 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2516
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
2517 /* 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
2518 int
b2fd7af17186 Expose dict_contains() and PyDict_Contains() with is about 10% faster
Raymond Hettinger <python@rcn.com>
parents: 30265
diff changeset
2519 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
2520 {
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
2521 Py_hash_t hash;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2522 PyDictObject *mp = (PyDictObject *)op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2523 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2524 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
2525
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2526 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
2527 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2528 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2529 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2530 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2531 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2532 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
2533 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
2534 }
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2535
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
2536 /* 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
2537 int
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
2538 _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
2539 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2540 PyDictObject *mp = (PyDictObject *)op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2541 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2542 PyObject **value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2543
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2544 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
2545 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
2546 }
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
2547
17594
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2548 /* 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
2549 static PySequenceMethods dict_as_sequence = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2550 0, /* sq_length */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2551 0, /* sq_concat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2552 0, /* sq_repeat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2553 0, /* sq_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2554 0, /* sq_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2555 0, /* sq_ass_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2556 0, /* sq_ass_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2557 PyDict_Contains, /* sq_contains */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2558 0, /* sq_inplace_concat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2559 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
2560 };
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2561
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2562 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2563 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
2564 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2565 PyObject *self;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2566
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2567 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
2568 self = type->tp_alloc(type, 0);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2569 if (self != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2570 PyDictObject *d = (PyDictObject *)self;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2571 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2572 /* XXX - Should we raise a no-memory error? */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2573 if (d->ma_keys == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2574 DK_INCREF(Py_EMPTY_KEYS);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2575 d->ma_keys = Py_EMPTY_KEYS;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2576 d->ma_values = empty_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2577 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2578 d->ma_used = 0;
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65754
diff changeset
2579 /* The object has been implicitly tracked by tp_alloc */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2580 if (type == &PyDict_Type)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2581 _PyObject_GC_UNTRACK(d);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2582 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2583 return self;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2584 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2585
19658
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2586 static int
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2587 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
2588 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2589 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
2590 }
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2591
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2592 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2593 dict_iter(PyDictObject *dict)
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2594 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2595 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
2596 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2597
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2598 PyDoc_STRVAR(dictionary_doc,
59224
8d48f9b59689 Merged revisions 78515-78516,78522 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55584
diff changeset
2599 "dict() -> new empty dictionary\n"
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20724
diff changeset
2600 "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
2601 " (key, value) pairs\n"
8d48f9b59689 Merged revisions 78515-78516,78522 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55584
diff changeset
2602 "dict(iterable) -> new dictionary initialized as if via:\n"
20724
f224632d0260 dictionary() constructor:
Tim Peters <tim.peters@gmail.com>
parents: 20683
diff changeset
2603 " d = {}\n"
59224
8d48f9b59689 Merged revisions 78515-78516,78522 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55584
diff changeset
2604 " 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
2605 " d[k] = v\n"
08b720cd387e Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents: 25115
diff changeset
2606 "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
2607 " 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
2608
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2609 PyTypeObject PyDict_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2610 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2611 "dict",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2612 sizeof(PyDictObject),
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2613 0,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2614 (destructor)dict_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2615 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2616 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2617 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2618 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2619 (reprfunc)dict_repr, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2620 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2621 &dict_as_sequence, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2622 &dict_as_mapping, /* tp_as_mapping */
65512
17c96aaf93c8 Remove unneeded casts to hashfunc.
Georg Brandl <georg@python.org>
parents: 65498
diff changeset
2623 PyObject_HashNotImplemented, /* tp_hash */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2624 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2625 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2626 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2627 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2628 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2629 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
2630 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
2631 dictionary_doc, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2632 dict_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2633 dict_tp_clear, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2634 dict_richcompare, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2635 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2636 (getiterfunc)dict_iter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2637 0, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2638 mapp_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2639 0, /* tp_members */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2640 0, /* tp_getset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2641 0, /* tp_base */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2642 0, /* tp_dict */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2643 0, /* tp_descr_get */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2644 0, /* tp_descr_set */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2645 0, /* tp_dictoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2646 dict_init, /* tp_init */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2647 PyType_GenericAlloc, /* tp_alloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2648 dict_new, /* tp_new */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2649 PyObject_GC_Del, /* tp_free */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2650 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2651
75959
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2652 PyObject *
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2653 _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
2654 {
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2655 PyObject *kv;
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2656 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
2657 if (kv == NULL)
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2658 return NULL;
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2659 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
2660 }
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2661
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
2662 /* 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
2663
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2664 PyObject *
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2665 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
2666 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2667 PyObject *kv, *rv;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2668 kv = PyUnicode_FromString(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2669 if (kv == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2670 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2671 rv = PyDict_GetItem(v, kv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2672 Py_DECREF(kv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2673 return rv;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2674 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2675
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2676 int
75959
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2677 _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
2678 {
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2679 PyObject *kv;
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2680 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
2681 if (kv == NULL)
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2682 return -1;
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2683 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
2684 }
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2685
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2686 int
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2687 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
2688 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2689 PyObject *kv;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2690 int err;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2691 kv = PyUnicode_FromString(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2692 if (kv == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2693 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2694 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
2695 err = PyDict_SetItem(v, kv, item);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2696 Py_DECREF(kv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2697 return err;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2698 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2699
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2700 int
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2701 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
2702 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2703 PyObject *kv;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2704 int err;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2705 kv = PyUnicode_FromString(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2706 if (kv == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2707 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2708 err = PyDict_DelItem(v, kv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2709 Py_DECREF(kv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2710 return err;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2711 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2712
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2713 /* Dictionary iterator types */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2714
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2715 typedef struct {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2716 PyObject_HEAD
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2717 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
2718 Py_ssize_t di_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2719 Py_ssize_t di_pos;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2720 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
2721 Py_ssize_t len;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2722 } dictiterobject;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2723
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2724 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2725 dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2726 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2727 dictiterobject *di;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2728 di = PyObject_GC_New(dictiterobject, itertype);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2729 if (di == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2730 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2731 Py_INCREF(dict);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2732 di->di_dict = dict;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2733 di->di_used = dict->ma_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2734 di->di_pos = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2735 di->len = dict->ma_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2736 if (itertype == &PyDictIterItem_Type) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2737 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
2738 if (di->di_result == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2739 Py_DECREF(di);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2740 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2741 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2742 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2743 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2744 di->di_result = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2745 _PyObject_GC_TRACK(di);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2746 return (PyObject *)di;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2747 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2748
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2749 static void
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2750 dictiter_dealloc(dictiterobject *di)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2751 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2752 Py_XDECREF(di->di_dict);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2753 Py_XDECREF(di->di_result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2754 PyObject_GC_Del(di);
50553
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
2755 }
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
2756
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
2757 static int
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
2758 dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
2759 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2760 Py_VISIT(di->di_dict);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2761 Py_VISIT(di->di_result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2762 return 0;
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
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2765 static PyObject *
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2766 dictiter_len(dictiterobject *di)
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2767 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2768 Py_ssize_t len = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2769 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
2770 len = di->len;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2771 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
2772 }
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2773
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2774 PyDoc_STRVAR(length_hint_doc,
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2775 "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
2776
76091
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
2777 static PyObject *
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
2778 dictiter_reduce(dictiterobject *di);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
2779
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
2780 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
2781
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2782 static PyMethodDef dictiter_methods[] = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2783 {"__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
2784 length_hint_doc},
76091
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
2785 {"__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
2786 reduce_doc},
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2787 {NULL, NULL} /* sentinel */
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2788 };
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2789
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2790 static PyObject *dictiter_iternextkey(dictiterobject *di)
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2791 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2792 PyObject *key;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2793 register Py_ssize_t i, mask, offset;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2794 register PyDictKeysObject *k;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2795 PyDictObject *d = di->di_dict;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2796 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
2797
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2798 if (d == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2799 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2800 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
2801
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2802 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
2803 PyErr_SetString(PyExc_RuntimeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2804 "dictionary changed size during iteration");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2805 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
2806 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2807 }
24259
2f60f90ce309 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2808
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2809 i = di->di_pos;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2810 if (i < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2811 goto fail;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2812 k = d->ma_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2813 if (d->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2814 value_ptr = &d->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2815 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2816 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2817 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2818 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
2819 offset = sizeof(PyDictKeyEntry);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2820 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2821 mask = DK_SIZE(k)-1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2822 while (i <= mask && *value_ptr == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2823 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
2824 i++;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2825 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2826 di->di_pos = i+1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2827 if (i > mask)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2828 goto fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2829 di->len--;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2830 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
2831 Py_INCREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2832 return key;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2833
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2834 fail:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2835 Py_DECREF(d);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2836 di->di_dict = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2837 return NULL;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2838 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2839
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2840 PyTypeObject PyDictIterKey_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2841 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2842 "dict_keyiterator", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2843 sizeof(dictiterobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2844 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2845 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2846 (destructor)dictiter_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2847 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2848 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2849 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2850 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2851 0, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2852 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2853 0, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2854 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2855 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2856 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2857 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2858 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2859 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2860 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2861 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
2862 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2863 (traverseproc)dictiter_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2864 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2865 0, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2866 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2867 PyObject_SelfIter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2868 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2869 dictiter_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2870 0,
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2871 };
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2872
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2873 static PyObject *dictiter_iternextvalue(dictiterobject *di)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2874 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2875 PyObject *value;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2876 register 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
2877 PyDictObject *d = di->di_dict;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2878 PyObject **value_ptr;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2879
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2880 if (d == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2881 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2882 assert (PyDict_Check(d));
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2883
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2884 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
2885 PyErr_SetString(PyExc_RuntimeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2886 "dictionary changed size during iteration");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2887 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
2888 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2889 }
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2890
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2891 i = di->di_pos;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2892 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
2893 if (i < 0 || i > mask)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2894 goto fail;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2895 if (d->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2896 value_ptr = &d->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2897 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2898 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2899 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2900 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
2901 offset = sizeof(PyDictKeyEntry);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2902 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2903 while (i <= mask && *value_ptr == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2904 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
2905 i++;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2906 if (i > mask)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2907 goto fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2908 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2909 di->di_pos = i+1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2910 di->len--;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2911 value = *value_ptr;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2912 Py_INCREF(value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2913 return value;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2914
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2915 fail:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2916 Py_DECREF(d);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2917 di->di_dict = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2918 return NULL;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2919 }
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 PyTypeObject PyDictIterValue_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2922 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2923 "dict_valueiterator", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2924 sizeof(dictiterobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2925 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2926 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2927 (destructor)dictiter_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2928 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2929 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2930 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2931 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2932 0, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2933 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2934 0, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2935 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2936 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2937 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2938 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2939 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2940 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2941 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2942 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
2943 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2944 (traverseproc)dictiter_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2945 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2946 0, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2947 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2948 PyObject_SelfIter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2949 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2950 dictiter_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2951 0,
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2952 };
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2953
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2954 static PyObject *dictiter_iternextitem(dictiterobject *di)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2955 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2956 PyObject *key, *value, *result = di->di_result;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2957 register 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
2958 PyDictObject *d = di->di_dict;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2959 PyObject **value_ptr;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2960
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2961 if (d == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2962 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2963 assert (PyDict_Check(d));
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2964
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2965 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
2966 PyErr_SetString(PyExc_RuntimeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2967 "dictionary changed size during iteration");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2968 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
2969 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2970 }
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2971
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2972 i = di->di_pos;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2973 if (i < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2974 goto fail;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2975 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
2976 if (d->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2977 value_ptr = &d->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2978 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2979 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2980 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2981 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
2982 offset = sizeof(PyDictKeyEntry);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2983 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2984 while (i <= mask && *value_ptr == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2985 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
2986 i++;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2987 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2988 di->di_pos = i+1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2989 if (i > mask)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2990 goto fail;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2991
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2992 if (result->ob_refcnt == 1) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2993 Py_INCREF(result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2994 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
2995 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
2996 } else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2997 result = PyTuple_New(2);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2998 if (result == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2999 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3000 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3001 di->len--;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3002 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
3003 value = *value_ptr;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3004 Py_INCREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3005 Py_INCREF(value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3006 PyTuple_SET_ITEM(result, 0, key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3007 PyTuple_SET_ITEM(result, 1, value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3008 return result;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3009
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3010 fail:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3011 Py_DECREF(d);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3012 di->di_dict = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3013 return NULL;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3014 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3015
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3016 PyTypeObject PyDictIterItem_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3017 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3018 "dict_itemiterator", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3019 sizeof(dictiterobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3020 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3021 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3022 (destructor)dictiter_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3023 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3024 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3025 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3026 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3027 0, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3028 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3029 0, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3030 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3031 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3032 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3033 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3034 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3035 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3036 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3037 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
3038 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3039 (traverseproc)dictiter_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3040 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3041 0, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3042 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3043 PyObject_SelfIter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3044 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3045 dictiter_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3046 0,
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3047 };
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3048
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3049
76091
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3050 static PyObject *
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3051 dictiter_reduce(dictiterobject *di)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3052 {
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3053 PyObject *list;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3054 dictiterobject tmp;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3055
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3056 list = PyList_New(0);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3057 if (!list)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3058 return NULL;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3059
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3060 /* copy the itertor state */
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3061 tmp = *di;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3062 Py_XINCREF(tmp.di_dict);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3063
76091
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3064 /* 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
3065 for(;;) {
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3066 PyObject *element = 0;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3067 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
3068 element = dictiter_iternextitem(&tmp);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3069 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
3070 element = dictiter_iternextkey(&tmp);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3071 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
3072 element = dictiter_iternextvalue(&tmp);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3073 else
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3074 assert(0);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3075 if (element) {
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3076 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
3077 Py_DECREF(element);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3078 Py_DECREF(list);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3079 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
3080 return NULL;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3081 }
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3082 Py_DECREF(element);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3083 } else
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3084 break;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3085 }
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3086 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
3087 /* check for error */
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3088 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
3089 /* we have an error */
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3090 Py_DECREF(list);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3091 return NULL;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3092 }
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
3093 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
3094 }
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3095
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3096 /***********************************************/
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3097 /* 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
3098 /***********************************************/
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3099
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3100 /* 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
3101
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3102 typedef struct {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3103 PyObject_HEAD
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3104 PyDictObject *dv_dict;
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3105 } dictviewobject;
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3106
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3107
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3108 static void
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3109 dictview_dealloc(dictviewobject *dv)
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3110 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3111 Py_XDECREF(dv->dv_dict);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3112 PyObject_GC_Del(dv);
50553
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
3113 }
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
3114
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
3115 static int
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
3116 dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
3117 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3118 Py_VISIT(dv->dv_dict);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3119 return 0;
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3120 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3121
40805
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3122 static Py_ssize_t
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3123 dictview_len(dictviewobject *dv)
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3124 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3125 Py_ssize_t len = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3126 if (dv->dv_dict != NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3127 len = dv->dv_dict->ma_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3128 return len;
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3129 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3130
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3131 static PyObject *
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3132 dictview_new(PyObject *dict, PyTypeObject *type)
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3133 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3134 dictviewobject *dv;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3135 if (dict == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3136 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3137 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3138 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3139 if (!PyDict_Check(dict)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3140 /* 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
3141 PyErr_Format(PyExc_TypeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3142 "%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
3143 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
3144 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3145 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3146 dv = PyObject_GC_New(dictviewobject, type);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3147 if (dv == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3148 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3149 Py_INCREF(dict);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3150 dv->dv_dict = (PyDictObject *)dict;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3151 _PyObject_GC_TRACK(dv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3152 return (PyObject *)dv;
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3153 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3154
40953
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3155 /* TODO(guido): The views objects are not complete:
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3156
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3157 * support more set operations
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3158 * support arbitrary mappings?
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3159 - either these should be static or exported in dictobject.h
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3160 - if public then they should probably be in builtins
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3161 */
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3162
42799
d02610b0a9f3 Patch with Keir Mierle: add rich comparisons between dict views and sets,
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
3163 /* 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
3164 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
3165 static int
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3166 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
3167 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3168 PyObject *iter = PyObject_GetIter(self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3169 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
3170
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3171 if (iter == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3172 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3173 for (;;) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3174 PyObject *next = PyIter_Next(iter);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3175 if (next == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3176 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3177 ok = -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3178 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3179 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3180 ok = PySequence_Contains(other, next);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3181 Py_DECREF(next);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3182 if (ok <= 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3183 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3184 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3185 Py_DECREF(iter);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3186 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
3187 }
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3188
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3189 static PyObject *
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3190 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
3191 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3192 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
3193 int ok;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3194 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
3195
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3196 assert(self != NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3197 assert(PyDictViewSet_Check(self));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3198 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
3199
71810
d0b0fcbb40db Replace Py_NotImplemented returns with the macro form Py_RETURN_NOTIMPLEMENTED.
Brian Curtin <brian@python.org>
parents: 68586
diff changeset
3200 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
3201 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
3202
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3203 len_self = PyObject_Size(self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3204 if (len_self < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3205 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3206 len_other = PyObject_Size(other);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3207 if (len_other < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3208 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
3209
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3210 ok = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3211 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
3212
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3213 case Py_NE:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3214 case Py_EQ:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3215 if (len_self == len_other)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3216 ok = all_contained_in(self, other);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3217 if (op == Py_NE && ok >= 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3218 ok = !ok;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3219 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
3220
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3221 case Py_LT:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3222 if (len_self < len_other)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3223 ok = all_contained_in(self, other);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3224 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
3225
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3226 case Py_LE:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3227 if (len_self <= len_other)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3228 ok = all_contained_in(self, other);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3229 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
3230
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3231 case Py_GT:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3232 if (len_self > len_other)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3233 ok = all_contained_in(other, self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3234 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
3235
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3236 case Py_GE:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3237 if (len_self >= len_other)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3238 ok = all_contained_in(other, self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3239 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
3240
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3241 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3242 if (ok < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3243 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3244 result = ok ? Py_True : Py_False;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3245 Py_INCREF(result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3246 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
3247 }
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3248
51971
5ad88b919318 Give dict views a helpful __repr__.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
3249 static PyObject *
5ad88b919318 Give dict views a helpful __repr__.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
3250 dictview_repr(dictviewobject *dv)
5ad88b919318 Give dict views a helpful __repr__.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
3251 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3252 PyObject *seq;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3253 PyObject *result;
51971
5ad88b919318 Give dict views a helpful __repr__.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
3254
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3255 seq = PySequence_List((PyObject *)dv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3256 if (seq == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3257 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3258
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3259 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
3260 Py_DECREF(seq);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3261 return result;
51971
5ad88b919318 Give dict views a helpful __repr__.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
3262 }
5ad88b919318 Give dict views a helpful __repr__.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
3263
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3264 /*** dict_keys ***/
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3265
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3266 static PyObject *
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3267 dictkeys_iter(dictviewobject *dv)
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3268 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3269 if (dv->dv_dict == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3270 Py_RETURN_NONE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3271 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3272 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
3273 }
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3274
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3275 static int
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3276 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
3277 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3278 if (dv->dv_dict == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3279 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3280 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
3281 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3282
40805
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3283 static PySequenceMethods dictkeys_as_sequence = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3284 (lenfunc)dictview_len, /* sq_length */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3285 0, /* sq_concat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3286 0, /* sq_repeat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3287 0, /* sq_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3288 0, /* sq_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3289 0, /* sq_ass_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3290 0, /* sq_ass_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3291 (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
3292 };
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3293
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
3294 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
3295 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
3296 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3297 PyObject *result = PySet_New(self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3298 PyObject *tmp;
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72914
diff changeset
3299 _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
3300
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3301 if (result == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3302 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
3303
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
3304 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
3305 if (tmp == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3306 Py_DECREF(result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3307 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3308 }
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
3309
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3310 Py_DECREF(tmp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3311 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
3312 }
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3313
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3314 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
3315 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
3316 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3317 PyObject *result = PySet_New(self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3318 PyObject *tmp;
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72914
diff changeset
3319 _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
3320
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3321 if (result == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3322 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
3323
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
3324 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
3325 if (tmp == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3326 Py_DECREF(result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3327 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3328 }
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
3329
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3330 Py_DECREF(tmp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3331 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
3332 }
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3333
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3334 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
3335 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
3336 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3337 PyObject *result = PySet_New(self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3338 PyObject *tmp;
72926
db4aa878cea2 Port SetAttrString/HasAttrString to SetAttrId/GetAttrId.
Martin v. Löwis <martin@v.loewis.de>
parents: 72925
diff changeset
3339 _Py_IDENTIFIER(update);
72914
d5c05e90be4c dictviews_or() uses _Py_identifier
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72835
diff changeset
3340
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3341 if (result == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3342 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
3343
72914
d5c05e90be4c dictviews_or() uses _Py_identifier
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72835
diff changeset
3344 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
3345 if (tmp == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3346 Py_DECREF(result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3347 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3348 }
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
3349
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3350 Py_DECREF(tmp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3351 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
3352 }
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3353
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3354 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
3355 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
3356 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3357 PyObject *result = PySet_New(self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3358 PyObject *tmp;
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72914
diff changeset
3359 _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
3360
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3361 if (result == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3362 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
3363
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
3364 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
3365 other);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3366 if (tmp == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3367 Py_DECREF(result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3368 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3369 }
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
3370
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3371 Py_DECREF(tmp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3372 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
3373 }
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3374
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3375 static PyNumberMethods dictviews_as_number = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3376 0, /*nb_add*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3377 (binaryfunc)dictviews_sub, /*nb_subtract*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3378 0, /*nb_multiply*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3379 0, /*nb_remainder*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3380 0, /*nb_divmod*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3381 0, /*nb_power*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3382 0, /*nb_negative*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3383 0, /*nb_positive*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3384 0, /*nb_absolute*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3385 0, /*nb_bool*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3386 0, /*nb_invert*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3387 0, /*nb_lshift*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3388 0, /*nb_rshift*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3389 (binaryfunc)dictviews_and, /*nb_and*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3390 (binaryfunc)dictviews_xor, /*nb_xor*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3391 (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
3392 };
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3393
64377
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3394 static PyObject*
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3395 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
3396 {
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3397 PyObject *it;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3398 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
3399
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3400 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
3401 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
3402 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
3403 else
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3404 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
3405 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3406
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3407 /* 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
3408 * 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
3409 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
3410 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
3411 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
3412 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
3413 return NULL;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3414
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3415 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
3416 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
3417 other = self;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3418 self = tmp;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3419 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3420 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3421
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3422 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
3423 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
3424 return NULL;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3425
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3426 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
3427 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
3428 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
3429 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
3430 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
3431 return NULL;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3432 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3433
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3434 if (contains) {
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3435 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
3436 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
3437 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3438 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3439 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
3440 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
3441 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
3442 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
3443 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3444
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3445 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
3446 "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
3447
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3448 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
3449 {"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
3450 isdisjoint_doc},
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3451 {NULL, NULL} /* sentinel */
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3452 };
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3453
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3454 PyTypeObject PyDictKeys_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3455 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3456 "dict_keys", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3457 sizeof(dictviewobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3458 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3459 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3460 (destructor)dictview_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3461 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3462 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3463 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3464 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3465 (reprfunc)dictview_repr, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3466 &dictviews_as_number, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3467 &dictkeys_as_sequence, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3468 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3469 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3470 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3471 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3472 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3473 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3474 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3475 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
3476 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3477 (traverseproc)dictview_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3478 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3479 dictview_richcompare, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3480 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3481 (getiterfunc)dictkeys_iter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3482 0, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3483 dictkeys_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3484 0,
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3485 };
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3486
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3487 static PyObject *
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3488 dictkeys_new(PyObject *dict)
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3489 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3490 return dictview_new(dict, &PyDictKeys_Type);
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3491 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3492
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3493 /*** dict_items ***/
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3494
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3495 static PyObject *
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3496 dictitems_iter(dictviewobject *dv)
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3497 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3498 if (dv->dv_dict == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3499 Py_RETURN_NONE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3500 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3501 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
3502 }
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3503
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3504 static int
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3505 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
3506 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3507 PyObject *key, *value, *found;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3508 if (dv->dv_dict == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3509 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3510 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
3511 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3512 key = PyTuple_GET_ITEM(obj, 0);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3513 value = PyTuple_GET_ITEM(obj, 1);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3514 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
3515 if (found == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3516 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3517 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3518 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3519 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3520 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
3521 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3522
40805
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3523 static PySequenceMethods dictitems_as_sequence = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3524 (lenfunc)dictview_len, /* sq_length */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3525 0, /* sq_concat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3526 0, /* sq_repeat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3527 0, /* sq_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3528 0, /* sq_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3529 0, /* sq_ass_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3530 0, /* sq_ass_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3531 (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
3532 };
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3533
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3534 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
3535 {"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
3536 isdisjoint_doc},
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3537 {NULL, NULL} /* sentinel */
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3538 };
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3539
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3540 PyTypeObject PyDictItems_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3541 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3542 "dict_items", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3543 sizeof(dictviewobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3544 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3545 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3546 (destructor)dictview_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3547 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3548 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3549 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3550 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3551 (reprfunc)dictview_repr, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3552 &dictviews_as_number, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3553 &dictitems_as_sequence, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3554 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3555 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3556 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3557 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3558 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3559 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3560 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3561 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
3562 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3563 (traverseproc)dictview_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3564 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3565 dictview_richcompare, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3566 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3567 (getiterfunc)dictitems_iter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3568 0, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3569 dictitems_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3570 0,
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3571 };
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3572
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3573 static PyObject *
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3574 dictitems_new(PyObject *dict)
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3575 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3576 return dictview_new(dict, &PyDictItems_Type);
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3577 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3578
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3579 /*** dict_values ***/
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3580
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3581 static PyObject *
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3582 dictvalues_iter(dictviewobject *dv)
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3583 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3584 if (dv->dv_dict == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3585 Py_RETURN_NONE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3586 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3587 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
3588 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3589
40805
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3590 static PySequenceMethods dictvalues_as_sequence = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3591 (lenfunc)dictview_len, /* sq_length */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3592 0, /* sq_concat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3593 0, /* sq_repeat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3594 0, /* sq_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3595 0, /* sq_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3596 0, /* sq_ass_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3597 0, /* sq_ass_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3598 (objobjproc)0, /* sq_contains */
40805
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3599 };
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3600
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3601 static PyMethodDef dictvalues_methods[] = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3602 {NULL, NULL} /* sentinel */
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3603 };
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3604
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3605 PyTypeObject PyDictValues_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3606 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3607 "dict_values", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3608 sizeof(dictviewobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3609 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3610 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3611 (destructor)dictview_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3612 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3613 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3614 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3615 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3616 (reprfunc)dictview_repr, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3617 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3618 &dictvalues_as_sequence, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3619 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3620 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3621 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3622 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3623 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3624 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3625 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3626 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
3627 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3628 (traverseproc)dictview_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3629 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3630 0, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3631 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3632 (getiterfunc)dictvalues_iter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3633 0, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3634 dictvalues_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3635 0,
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3636 };
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 static PyObject *
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3639 dictvalues_new(PyObject *dict)
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3640 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3641 return dictview_new(dict, &PyDictValues_Type);
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3642 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3643
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3644 /* 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
3645 but does not set an error */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3646 PyDictKeysObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3647 _PyDict_NewKeysForClass(void)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3648 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3649 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
3650 if (keys == NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3651 PyErr_Clear();
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3652 else
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3653 keys->dk_lookup = lookdict_split;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3654 return keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3655 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3656
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3657 #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
3658
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3659 PyObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3660 PyObject_GenericGetDict(PyObject *obj, void *context)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3661 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3662 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3663 if (dictptr == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3664 PyErr_SetString(PyExc_AttributeError,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3665 "This object has no __dict__");
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3666 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3667 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3668 dict = *dictptr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3669 if (dict == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3670 PyTypeObject *tp = Py_TYPE(obj);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3671 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
3672 DK_INCREF(CACHED_KEYS(tp));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3673 *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
3674 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3675 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3676 *dictptr = dict = PyDict_New();
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3677 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3678 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3679 Py_XINCREF(dict);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3680 return dict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3681 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3682
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3683 int
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3684 _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3685 PyObject *key, PyObject *value)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3686 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3687 PyObject *dict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3688 int res;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3689 PyDictKeysObject *cached;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3690
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3691 assert(dictptr != NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3692 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
3693 assert(dictptr != NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3694 dict = *dictptr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3695 if (dict == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3696 DK_INCREF(cached);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3697 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
3698 if (dict == NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3699 return -1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3700 *dictptr = dict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3701 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3702 if (value == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3703 res = PyDict_DelItem(dict, key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3704 if (cached != ((PyDictObject *)dict)->ma_keys) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3705 CACHED_KEYS(tp) = NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3706 DK_DECREF(cached);
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 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3709 res = PyDict_SetItem(dict, key, value);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3710 if (cached != ((PyDictObject *)dict)->ma_keys) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3711 /* 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
3712 if (cached->dk_refcnt == 1) {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3713 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
3714 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3715 CACHED_KEYS(tp) = 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 DK_DECREF(cached);
76535
5d5b72a71898 distiguish between refusing to creating shared keys and error (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76530
diff changeset
3718 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
3719 return -1;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3720 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3721 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3722 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3723 dict = *dictptr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3724 if (dict == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3725 dict = PyDict_New();
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3726 if (dict == NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3727 return -1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3728 *dictptr = dict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3729 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3730 if (value == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3731 res = PyDict_DelItem(dict, key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3732 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3733 res = PyDict_SetItem(dict, key, value);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3734 }
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 return res;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3737 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3738
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3739 void
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3740 _PyDictKeys_DecRef(PyDictKeysObject *keys)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3741 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3742 DK_DECREF(keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3743 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3744
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3745
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3746 /* ARGSUSED */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3747 static PyObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3748 dummy_repr(PyObject *op)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3749 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3750 return PyUnicode_FromString("<dummy key>");
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3751 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3752
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3753 /* ARGUSED */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3754 static void
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3755 dummy_dealloc(PyObject* ignore)
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 /* 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
3758 * 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
3759 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3760 Py_FatalError("deallocating <dummy key>");
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3761 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3762
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3763 static PyTypeObject PyDictDummy_Type = {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3764 PyVarObject_HEAD_INIT(&PyType_Type, 0)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3765 "<dummy key> type",
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3766 0,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3767 0,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3768 dummy_dealloc, /*tp_dealloc*/ /*never called*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3769 0, /*tp_print*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3770 0, /*tp_getattr*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3771 0, /*tp_setattr*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3772 0, /*tp_reserved*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3773 dummy_repr, /*tp_repr*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3774 0, /*tp_as_number*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3775 0, /*tp_as_sequence*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3776 0, /*tp_as_mapping*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3777 0, /*tp_hash */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3778 0, /*tp_call */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3779 0, /*tp_str */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3780 0, /*tp_getattro */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3781 0, /*tp_setattro */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3782 0, /*tp_as_buffer */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3783 Py_TPFLAGS_DEFAULT, /*tp_flags */
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
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3786 static PyObject _dummy_struct = {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3787 _PyObject_EXTRA_INIT
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3788 2, &PyDictDummy_Type
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3789 };
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3790