Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 1 | /* Dictionary object implementation using a hash table */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2 | |
Raymond Hettinger | 930427b | 2003-05-03 06:51:59 +0000 | [diff] [blame] | 3 | /* The distribution includes a separate file, Objects/dictnotes.txt, |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 4 | describing explorations into dictionary design and optimization. |
Raymond Hettinger | 930427b | 2003-05-03 06:51:59 +0000 | [diff] [blame] | 5 | It covers typical dictionary use patterns, the parameters for |
| 6 | tuning dictionaries, and several ideas for possible optimizations. |
| 7 | */ |
| 8 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 9 | /* PyDictKeysObject |
| 10 | |
| 11 | This implements the dictionary's hashtable. |
| 12 | |
Raymond Hettinger | b12785d | 2016-10-22 09:58:14 -0700 | [diff] [blame] | 13 | As of Python 3.6, this is compact and ordered. Basic idea is described here: |
| 14 | * https://mail.python.org/pipermail/python-dev/2012-December/123028.html |
| 15 | * https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 16 | |
| 17 | layout: |
| 18 | |
| 19 | +---------------+ |
| 20 | | dk_refcnt | |
| 21 | | dk_size | |
| 22 | | dk_lookup | |
| 23 | | dk_usable | |
| 24 | | dk_nentries | |
| 25 | +---------------+ |
| 26 | | dk_indices | |
| 27 | | | |
| 28 | +---------------+ |
| 29 | | dk_entries | |
| 30 | | | |
| 31 | +---------------+ |
| 32 | |
| 33 | dk_indices is actual hashtable. It holds index in entries, or DKIX_EMPTY(-1) |
| 34 | or DKIX_DUMMY(-2). |
| 35 | Size of indices is dk_size. Type of each index in indices is vary on dk_size: |
| 36 | |
| 37 | * int8 for dk_size <= 128 |
| 38 | * int16 for 256 <= dk_size <= 2**15 |
| 39 | * int32 for 2**16 <= dk_size <= 2**31 |
| 40 | * int64 for 2**32 <= dk_size |
| 41 | |
| 42 | dk_entries is array of PyDictKeyEntry. It's size is USABLE_FRACTION(dk_size). |
| 43 | DK_ENTRIES(dk) can be used to get pointer to entries. |
| 44 | |
| 45 | NOTE: Since negative value is used for DKIX_EMPTY and DKIX_DUMMY, type of |
| 46 | dk_indices entry is signed integer and int16 is used for table which |
| 47 | dk_size == 256. |
| 48 | */ |
| 49 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 50 | |
| 51 | /* |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 52 | The DictObject can be in one of two forms. |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 53 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 54 | Either: |
| 55 | A combined table: |
| 56 | ma_values == NULL, dk_refcnt == 1. |
| 57 | Values are stored in the me_value field of the PyDictKeysObject. |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 58 | Or: |
| 59 | A split table: |
| 60 | ma_values != NULL, dk_refcnt >= 1 |
| 61 | Values are stored in the ma_values array. |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 62 | Only string (unicode) keys are allowed. |
| 63 | All dicts sharing same key must have same insertion order. |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 64 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 65 | There are four kinds of slots in the table (slot is index, and |
| 66 | DK_ENTRIES(keys)[index] if index >= 0): |
| 67 | |
| 68 | 1. Unused. index == DKIX_EMPTY |
| 69 | Does not hold an active (key, value) pair now and never did. Unused can |
| 70 | transition to Active upon key insertion. This is each slot's initial state. |
| 71 | |
| 72 | 2. Active. index >= 0, me_key != NULL and me_value != NULL |
| 73 | Holds an active (key, value) pair. Active can transition to Dummy or |
| 74 | Pending upon key deletion (for combined and split tables respectively). |
| 75 | This is the only case in which me_value != NULL. |
| 76 | |
| 77 | 3. Dummy. index == DKIX_DUMMY (combined only) |
| 78 | Previously held an active (key, value) pair, but that was deleted and an |
| 79 | active pair has not yet overwritten the slot. Dummy can transition to |
| 80 | Active upon key insertion. Dummy slots cannot be made Unused again |
| 81 | else the probe sequence in case of collision would have no way to know |
| 82 | they were once active. |
| 83 | |
| 84 | 4. Pending. index >= 0, key != NULL, and value == NULL (split only) |
| 85 | Not yet inserted in split-table. |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 86 | */ |
| 87 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 88 | /* |
| 89 | Preserving insertion order |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 90 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 91 | It's simple for combined table. Since dk_entries is mostly append only, we can |
| 92 | get insertion order by just iterating dk_entries. |
| 93 | |
| 94 | One exception is .popitem(). It removes last item in dk_entries and decrement |
| 95 | dk_nentries to achieve amortized O(1). Since there are DKIX_DUMMY remains in |
| 96 | dk_indices, we can't increment dk_usable even though dk_nentries is |
| 97 | decremented. |
| 98 | |
| 99 | In split table, inserting into pending entry is allowed only for dk_entries[ix] |
| 100 | where ix == mp->ma_used. Inserting into other index and deleting item cause |
| 101 | converting the dict to the combined table. |
| 102 | */ |
| 103 | |
| 104 | /* PyDict_MINSIZE is the starting size for any new dict. |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 105 | * 8 allows dicts with no more than 5 active entries; experiments suggested |
| 106 | * this suffices for the majority of dicts (consisting mostly of usually-small |
| 107 | * dicts created to pass keyword arguments). |
| 108 | * Making this 8, rather than 4 reduces the number of resizes for most |
| 109 | * dictionaries, without any significant extra memory use. |
| 110 | */ |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 111 | #define PyDict_MINSIZE 8 |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 112 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 113 | #include "Python.h" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 114 | #include "internal/pystate.h" |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 115 | #include "dict-common.h" |
Victor Stinner | 990397e | 2016-09-09 20:22:59 -0700 | [diff] [blame] | 116 | #include "stringlib/eq.h" /* to get unicode_eq() */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 117 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 118 | /*[clinic input] |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 119 | class dict "PyDictObject *" "&PyDict_Type" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 120 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 121 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/ |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 122 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | To ensure the lookup algorithm terminates, there must be at least one Unused |
| 126 | slot (NULL key) in the table. |
| 127 | To avoid slowing down lookups on a near-full table, we resize the table when |
| 128 | it's USABLE_FRACTION (currently two-thirds) full. |
| 129 | */ |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 130 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 131 | #define PERTURB_SHIFT 5 |
| 132 | |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 133 | /* |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 134 | Major subtleties ahead: Most hash schemes depend on having a "good" hash |
| 135 | function, in the sense of simulating randomness. Python doesn't: its most |
R David Murray | 537ad7a | 2016-07-10 12:33:18 -0400 | [diff] [blame] | 136 | important hash functions (for ints) are very regular in common |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 137 | cases: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 138 | |
R David Murray | 537ad7a | 2016-07-10 12:33:18 -0400 | [diff] [blame] | 139 | >>>[hash(i) for i in range(4)] |
Guido van Rossum | dc5f6b2 | 2006-08-24 21:29:26 +0000 | [diff] [blame] | 140 | [0, 1, 2, 3] |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 141 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 142 | This isn't necessarily bad! To the contrary, in a table of size 2**i, taking |
| 143 | the low-order i bits as the initial table index is extremely fast, and there |
R David Murray | 537ad7a | 2016-07-10 12:33:18 -0400 | [diff] [blame] | 144 | are no collisions at all for dicts indexed by a contiguous range of ints. So |
| 145 | this gives better-than-random behavior in common cases, and that's very |
| 146 | desirable. |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 147 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 148 | OTOH, when collisions occur, the tendency to fill contiguous slices of the |
| 149 | hash table makes a good collision resolution strategy crucial. Taking only |
| 150 | the last i bits of the hash code is also vulnerable: for example, consider |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | the list [i << 16 for i in range(20000)] as a set of keys. Since ints are |
Guido van Rossum | dc5f6b2 | 2006-08-24 21:29:26 +0000 | [diff] [blame] | 152 | their own hash codes, and this fits in a dict of size 2**15, the last 15 bits |
| 153 | of every hash code are all 0: they *all* map to the same table index. |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 154 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 155 | But catering to unusual cases should not slow the usual ones, so we just take |
| 156 | the last i bits anyway. It's up to collision resolution to do the rest. If |
| 157 | we *usually* find the key we're looking for on the first try (and, it turns |
| 158 | out, we usually do -- the table load factor is kept under 2/3, so the odds |
| 159 | are solidly in our favor), then it makes best sense to keep the initial index |
| 160 | computation dirt cheap. |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 161 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 162 | The first half of collision resolution is to visit table indices via this |
| 163 | recurrence: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 164 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 165 | j = ((5*j) + 1) mod 2**i |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 166 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 167 | For any initial j in range(2**i), repeating that 2**i times generates each |
| 168 | int in range(2**i) exactly once (see any text on random-number generation for |
| 169 | proof). By itself, this doesn't help much: like linear probing (setting |
| 170 | j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed |
| 171 | order. This would be bad, except that's not the only thing we do, and it's |
| 172 | actually *good* in the common cases where hash keys are consecutive. In an |
| 173 | example that's really too small to make this entirely clear, for a table of |
| 174 | size 2**3 the order of indices is: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 175 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 176 | 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating] |
| 177 | |
| 178 | If two things come in at index 5, the first place we look after is index 2, |
| 179 | not 6, so if another comes in at index 6 the collision at 5 didn't hurt it. |
| 180 | Linear probing is deadly in this case because there the fixed probe order |
| 181 | is the *same* as the order consecutive keys are likely to arrive. But it's |
| 182 | extremely unlikely hash codes will follow a 5*j+1 recurrence by accident, |
| 183 | and certain that consecutive hash codes do not. |
| 184 | |
| 185 | The other half of the strategy is to get the other bits of the hash code |
| 186 | into play. This is done by initializing a (unsigned) vrbl "perturb" to the |
| 187 | full hash code, and changing the recurrence to: |
| 188 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 189 | perturb >>= PERTURB_SHIFT; |
INADA Naoki | 267941c | 2016-10-06 15:19:07 +0900 | [diff] [blame] | 190 | j = (5*j) + 1 + perturb; |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 191 | use j % 2**i as the next table index; |
| 192 | |
| 193 | Now the probe sequence depends (eventually) on every bit in the hash code, |
| 194 | and the pseudo-scrambling property of recurring on 5*j+1 is more valuable, |
| 195 | because it quickly magnifies small differences in the bits that didn't affect |
| 196 | the initial index. Note that because perturb is unsigned, if the recurrence |
| 197 | is executed often enough perturb eventually becomes and remains 0. At that |
| 198 | point (very rarely reached) the recurrence is on (just) 5*j+1 again, and |
| 199 | that's certain to find an empty slot eventually (since it generates every int |
| 200 | in range(2**i), and we make sure there's always at least one empty slot). |
| 201 | |
| 202 | Selecting a good value for PERTURB_SHIFT is a balancing act. You want it |
| 203 | small so that the high bits of the hash code continue to affect the probe |
| 204 | sequence across iterations; but you want it large so that in really bad cases |
| 205 | the high-order hash bits have an effect on early iterations. 5 was "the |
| 206 | best" in minimizing total collisions across experiments Tim Peters ran (on |
| 207 | both normal and pathological cases), but 4 and 6 weren't significantly worse. |
| 208 | |
Guido van Rossum | dc5f6b2 | 2006-08-24 21:29:26 +0000 | [diff] [blame] | 209 | Historical: Reimer Behrends contributed the idea of using a polynomial-based |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 210 | approach, using repeated multiplication by x in GF(2**n) where an irreducible |
| 211 | polynomial for each table size was chosen such that x was a primitive root. |
| 212 | Christian Tismer later extended that to use division by x instead, as an |
| 213 | efficient way to get the high bits of the hash code into play. This scheme |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 214 | also gave excellent collision statistics, but was more expensive: two |
| 215 | if-tests were required inside the loop; computing "the next" index took about |
| 216 | the same number of operations but without as much potential parallelism |
| 217 | (e.g., computing 5*j can go on at the same time as computing 1+perturb in the |
| 218 | above, and then shifting perturb can be done while the table index is being |
| 219 | masked); and the PyDictObject struct required a member to hold the table's |
| 220 | polynomial. In Tim's experiments the current scheme ran faster, produced |
| 221 | equally good collision statistics, needed less code & used less memory. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 222 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 223 | */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 224 | |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 225 | /* forward declarations */ |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 226 | static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key, |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 227 | Py_hash_t hash, PyObject **value_addr); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 228 | static Py_ssize_t lookdict_unicode(PyDictObject *mp, PyObject *key, |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 229 | Py_hash_t hash, PyObject **value_addr); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 230 | static Py_ssize_t |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 231 | lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key, |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 232 | Py_hash_t hash, PyObject **value_addr); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 233 | static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key, |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 234 | Py_hash_t hash, PyObject **value_addr); |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 235 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 236 | static int dictresize(PyDictObject *mp, Py_ssize_t minused); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 237 | |
Benjamin Peterson | 3c56929 | 2016-09-08 13:16:41 -0700 | [diff] [blame] | 238 | /*Global counter used to set ma_version_tag field of dictionary. |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 239 | * It is incremented each time that a dictionary is created and each |
| 240 | * time that a dictionary is modified. */ |
| 241 | static uint64_t pydict_global_version = 0; |
| 242 | |
| 243 | #define DICT_NEXT_VERSION() (++pydict_global_version) |
| 244 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 245 | /* Dictionary reuse scheme to save calls to malloc and free */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 246 | #ifndef PyDict_MAXFREELIST |
| 247 | #define PyDict_MAXFREELIST 80 |
| 248 | #endif |
| 249 | static PyDictObject *free_list[PyDict_MAXFREELIST]; |
| 250 | static int numfree = 0; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 251 | static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST]; |
| 252 | static int numfreekeys = 0; |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 253 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 254 | #include "clinic/dictobject.c.h" |
| 255 | |
Antoine Pitrou | 9a812cb | 2011-11-15 00:00:12 +0100 | [diff] [blame] | 256 | int |
| 257 | PyDict_ClearFreeList(void) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | PyDictObject *op; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 260 | int ret = numfree + numfreekeys; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | while (numfree) { |
| 262 | op = free_list[--numfree]; |
| 263 | assert(PyDict_CheckExact(op)); |
| 264 | PyObject_GC_Del(op); |
| 265 | } |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 266 | while (numfreekeys) { |
| 267 | PyObject_FREE(keys_free_list[--numfreekeys]); |
| 268 | } |
Antoine Pitrou | 9a812cb | 2011-11-15 00:00:12 +0100 | [diff] [blame] | 269 | return ret; |
| 270 | } |
| 271 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 272 | /* Print summary info about the state of the optimized allocator */ |
| 273 | void |
| 274 | _PyDict_DebugMallocStats(FILE *out) |
| 275 | { |
| 276 | _PyDebugAllocatorStats(out, |
| 277 | "free PyDictObject", numfree, sizeof(PyDictObject)); |
| 278 | } |
| 279 | |
| 280 | |
Antoine Pitrou | 9a812cb | 2011-11-15 00:00:12 +0100 | [diff] [blame] | 281 | void |
| 282 | PyDict_Fini(void) |
| 283 | { |
| 284 | PyDict_ClearFreeList(); |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 287 | #define DK_SIZE(dk) ((dk)->dk_size) |
| 288 | #if SIZEOF_VOID_P > 4 |
Victor Stinner | 58f7c5a | 2016-09-08 11:37:36 -0700 | [diff] [blame] | 289 | #define DK_IXSIZE(dk) \ |
| 290 | (DK_SIZE(dk) <= 0xff ? \ |
| 291 | 1 : DK_SIZE(dk) <= 0xffff ? \ |
| 292 | 2 : DK_SIZE(dk) <= 0xffffffff ? \ |
Benjamin Peterson | 3c56929 | 2016-09-08 13:16:41 -0700 | [diff] [blame] | 293 | 4 : sizeof(int64_t)) |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 294 | #else |
Victor Stinner | 58f7c5a | 2016-09-08 11:37:36 -0700 | [diff] [blame] | 295 | #define DK_IXSIZE(dk) \ |
| 296 | (DK_SIZE(dk) <= 0xff ? \ |
| 297 | 1 : DK_SIZE(dk) <= 0xffff ? \ |
Benjamin Peterson | 3c56929 | 2016-09-08 13:16:41 -0700 | [diff] [blame] | 298 | 2 : sizeof(int32_t)) |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 299 | #endif |
Victor Stinner | 58f7c5a | 2016-09-08 11:37:36 -0700 | [diff] [blame] | 300 | #define DK_ENTRIES(dk) \ |
Gregory P. Smith | 397f1b2 | 2018-04-19 22:41:19 -0700 | [diff] [blame] | 301 | ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)])) |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 302 | |
Antoine Pitrou | 2d169b2 | 2012-05-12 23:43:44 +0200 | [diff] [blame] | 303 | #define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA |
| 304 | #define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA |
| 305 | |
| 306 | #define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt) |
| 307 | #define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 308 | #define DK_MASK(dk) (((dk)->dk_size)-1) |
| 309 | #define IS_POWER_OF_2(x) (((x) & (x-1)) == 0) |
| 310 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 311 | /* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */ |
Benjamin Peterson | 7322225 | 2016-09-08 09:58:47 -0700 | [diff] [blame] | 312 | static inline Py_ssize_t |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 313 | dk_get_index(PyDictKeysObject *keys, Py_ssize_t i) |
| 314 | { |
| 315 | Py_ssize_t s = DK_SIZE(keys); |
Victor Stinner | 71211e3 | 2016-09-08 10:52:46 -0700 | [diff] [blame] | 316 | Py_ssize_t ix; |
| 317 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 318 | if (s <= 0xff) { |
Gregory P. Smith | 397f1b2 | 2018-04-19 22:41:19 -0700 | [diff] [blame] | 319 | int8_t *indices = (int8_t*)(keys->dk_indices); |
Victor Stinner | 208857e | 2016-09-08 11:35:46 -0700 | [diff] [blame] | 320 | ix = indices[i]; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 321 | } |
| 322 | else if (s <= 0xffff) { |
Gregory P. Smith | 397f1b2 | 2018-04-19 22:41:19 -0700 | [diff] [blame] | 323 | int16_t *indices = (int16_t*)(keys->dk_indices); |
Victor Stinner | 208857e | 2016-09-08 11:35:46 -0700 | [diff] [blame] | 324 | ix = indices[i]; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 325 | } |
Benjamin Peterson | 3c56929 | 2016-09-08 13:16:41 -0700 | [diff] [blame] | 326 | #if SIZEOF_VOID_P > 4 |
Serhiy Storchaka | 473e0e4 | 2016-09-10 21:34:43 +0300 | [diff] [blame] | 327 | else if (s > 0xffffffff) { |
Gregory P. Smith | 397f1b2 | 2018-04-19 22:41:19 -0700 | [diff] [blame] | 328 | int64_t *indices = (int64_t*)(keys->dk_indices); |
Victor Stinner | 208857e | 2016-09-08 11:35:46 -0700 | [diff] [blame] | 329 | ix = indices[i]; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 330 | } |
Benjamin Peterson | 3c56929 | 2016-09-08 13:16:41 -0700 | [diff] [blame] | 331 | #endif |
Serhiy Storchaka | 473e0e4 | 2016-09-10 21:34:43 +0300 | [diff] [blame] | 332 | else { |
Gregory P. Smith | 397f1b2 | 2018-04-19 22:41:19 -0700 | [diff] [blame] | 333 | int32_t *indices = (int32_t*)(keys->dk_indices); |
Serhiy Storchaka | 473e0e4 | 2016-09-10 21:34:43 +0300 | [diff] [blame] | 334 | ix = indices[i]; |
| 335 | } |
Victor Stinner | 71211e3 | 2016-09-08 10:52:46 -0700 | [diff] [blame] | 336 | assert(ix >= DKIX_DUMMY); |
| 337 | return ix; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /* write to indices. */ |
Benjamin Peterson | 7322225 | 2016-09-08 09:58:47 -0700 | [diff] [blame] | 341 | static inline void |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 342 | dk_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix) |
| 343 | { |
| 344 | Py_ssize_t s = DK_SIZE(keys); |
Victor Stinner | 71211e3 | 2016-09-08 10:52:46 -0700 | [diff] [blame] | 345 | |
| 346 | assert(ix >= DKIX_DUMMY); |
| 347 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 348 | if (s <= 0xff) { |
Gregory P. Smith | 397f1b2 | 2018-04-19 22:41:19 -0700 | [diff] [blame] | 349 | int8_t *indices = (int8_t*)(keys->dk_indices); |
Victor Stinner | 71211e3 | 2016-09-08 10:52:46 -0700 | [diff] [blame] | 350 | assert(ix <= 0x7f); |
Victor Stinner | 208857e | 2016-09-08 11:35:46 -0700 | [diff] [blame] | 351 | indices[i] = (char)ix; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 352 | } |
| 353 | else if (s <= 0xffff) { |
Gregory P. Smith | 397f1b2 | 2018-04-19 22:41:19 -0700 | [diff] [blame] | 354 | int16_t *indices = (int16_t*)(keys->dk_indices); |
Victor Stinner | 71211e3 | 2016-09-08 10:52:46 -0700 | [diff] [blame] | 355 | assert(ix <= 0x7fff); |
Victor Stinner | 208857e | 2016-09-08 11:35:46 -0700 | [diff] [blame] | 356 | indices[i] = (int16_t)ix; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 357 | } |
Benjamin Peterson | 3c56929 | 2016-09-08 13:16:41 -0700 | [diff] [blame] | 358 | #if SIZEOF_VOID_P > 4 |
Serhiy Storchaka | 473e0e4 | 2016-09-10 21:34:43 +0300 | [diff] [blame] | 359 | else if (s > 0xffffffff) { |
Gregory P. Smith | 397f1b2 | 2018-04-19 22:41:19 -0700 | [diff] [blame] | 360 | int64_t *indices = (int64_t*)(keys->dk_indices); |
Victor Stinner | 208857e | 2016-09-08 11:35:46 -0700 | [diff] [blame] | 361 | indices[i] = ix; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 362 | } |
Benjamin Peterson | 3c56929 | 2016-09-08 13:16:41 -0700 | [diff] [blame] | 363 | #endif |
Serhiy Storchaka | 473e0e4 | 2016-09-10 21:34:43 +0300 | [diff] [blame] | 364 | else { |
Gregory P. Smith | 397f1b2 | 2018-04-19 22:41:19 -0700 | [diff] [blame] | 365 | int32_t *indices = (int32_t*)(keys->dk_indices); |
Serhiy Storchaka | 473e0e4 | 2016-09-10 21:34:43 +0300 | [diff] [blame] | 366 | assert(ix <= 0x7fffffff); |
| 367 | indices[i] = (int32_t)ix; |
| 368 | } |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | |
Antoine Pitrou | a504a7a | 2012-06-24 21:03:45 +0200 | [diff] [blame] | 372 | /* USABLE_FRACTION is the maximum dictionary load. |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 373 | * Increasing this ratio makes dictionaries more dense resulting in more |
| 374 | * collisions. Decreasing it improves sparseness at the expense of spreading |
| 375 | * indices over more cache lines and at the cost of total memory consumed. |
Antoine Pitrou | a504a7a | 2012-06-24 21:03:45 +0200 | [diff] [blame] | 376 | * |
| 377 | * USABLE_FRACTION must obey the following: |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 378 | * (0 < USABLE_FRACTION(n) < n) for all n >= 2 |
| 379 | * |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 380 | * USABLE_FRACTION should be quick to calculate. |
| 381 | * Fractions around 1/2 to 2/3 seem to work well in practice. |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 382 | */ |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 383 | #define USABLE_FRACTION(n) (((n) << 1)/3) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 384 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 385 | /* ESTIMATE_SIZE is reverse function of USABLE_FRACTION. |
| 386 | * This can be used to reserve enough size to insert n entries without |
| 387 | * resizing. |
| 388 | */ |
INADA Naoki | 92c50ee | 2016-11-22 00:57:02 +0900 | [diff] [blame] | 389 | #define ESTIMATE_SIZE(n) (((n)*3+1) >> 1) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 390 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 391 | /* Alternative fraction that is otherwise close enough to 2n/3 to make |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 392 | * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10. |
| 393 | * 32 * 2/3 = 21, 32 * 5/8 = 20. |
| 394 | * Its advantage is that it is faster to compute on machines with slow division. |
| 395 | * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3)) |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 396 | */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 397 | |
Victor Stinner | a9f61a5 | 2013-07-16 22:17:26 +0200 | [diff] [blame] | 398 | /* GROWTH_RATE. Growth rate upon hitting maximum load. |
INADA Naoki | 5fbc511 | 2018-04-17 15:53:34 +0900 | [diff] [blame] | 399 | * Currently set to used*3. |
Victor Stinner | a9f61a5 | 2013-07-16 22:17:26 +0200 | [diff] [blame] | 400 | * This means that dicts double in size when growing without deletions, |
Raymond Hettinger | 36f74aa | 2013-05-17 03:01:13 -0700 | [diff] [blame] | 401 | * but have more head room when the number of deletions is on a par with the |
INADA Naoki | 5fbc511 | 2018-04-17 15:53:34 +0900 | [diff] [blame] | 402 | * number of insertions. See also bpo-17563 and bpo-33205. |
| 403 | * |
Raymond Hettinger | 36f74aa | 2013-05-17 03:01:13 -0700 | [diff] [blame] | 404 | * GROWTH_RATE was set to used*4 up to version 3.2. |
| 405 | * GROWTH_RATE was set to used*2 in version 3.3.0 |
INADA Naoki | 5fbc511 | 2018-04-17 15:53:34 +0900 | [diff] [blame] | 406 | * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0. |
Antoine Pitrou | a504a7a | 2012-06-24 21:03:45 +0200 | [diff] [blame] | 407 | */ |
INADA Naoki | 5fbc511 | 2018-04-17 15:53:34 +0900 | [diff] [blame] | 408 | #define GROWTH_RATE(d) ((d)->ma_used*3) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 409 | |
| 410 | #define ENSURE_ALLOWS_DELETIONS(d) \ |
| 411 | if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \ |
| 412 | (d)->ma_keys->dk_lookup = lookdict_unicode; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 414 | |
| 415 | /* This immutable, empty PyDictKeysObject is used for PyDict_Clear() |
| 416 | * (which cannot fail and thus can do no allocation). |
| 417 | */ |
| 418 | static PyDictKeysObject empty_keys_struct = { |
Serhiy Storchaka | 97932e4 | 2016-09-26 23:01:23 +0300 | [diff] [blame] | 419 | 1, /* dk_refcnt */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 420 | 1, /* dk_size */ |
| 421 | lookdict_split, /* dk_lookup */ |
| 422 | 0, /* dk_usable (immutable) */ |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 423 | 0, /* dk_nentries */ |
Gregory P. Smith | 397f1b2 | 2018-04-19 22:41:19 -0700 | [diff] [blame] | 424 | {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, |
| 425 | DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 426 | }; |
| 427 | |
| 428 | static PyObject *empty_values[1] = { NULL }; |
| 429 | |
| 430 | #define Py_EMPTY_KEYS &empty_keys_struct |
| 431 | |
Victor Stinner | 611b0fa | 2016-09-14 15:02:01 +0200 | [diff] [blame] | 432 | /* Uncomment to check the dict content in _PyDict_CheckConsistency() */ |
| 433 | /* #define DEBUG_PYDICT */ |
| 434 | |
| 435 | |
T. Wouters | a00c3fd | 2017-03-31 09:14:41 -0700 | [diff] [blame] | 436 | #ifndef NDEBUG |
Victor Stinner | 611b0fa | 2016-09-14 15:02:01 +0200 | [diff] [blame] | 437 | static int |
| 438 | _PyDict_CheckConsistency(PyDictObject *mp) |
| 439 | { |
| 440 | PyDictKeysObject *keys = mp->ma_keys; |
| 441 | int splitted = _PyDict_HasSplitTable(mp); |
| 442 | Py_ssize_t usable = USABLE_FRACTION(keys->dk_size); |
| 443 | #ifdef DEBUG_PYDICT |
| 444 | PyDictKeyEntry *entries = DK_ENTRIES(keys); |
| 445 | Py_ssize_t i; |
| 446 | #endif |
| 447 | |
| 448 | assert(0 <= mp->ma_used && mp->ma_used <= usable); |
| 449 | assert(IS_POWER_OF_2(keys->dk_size)); |
| 450 | assert(0 <= keys->dk_usable |
| 451 | && keys->dk_usable <= usable); |
| 452 | assert(0 <= keys->dk_nentries |
| 453 | && keys->dk_nentries <= usable); |
| 454 | assert(keys->dk_usable + keys->dk_nentries <= usable); |
| 455 | |
| 456 | if (!splitted) { |
| 457 | /* combined table */ |
| 458 | assert(keys->dk_refcnt == 1); |
| 459 | } |
| 460 | |
| 461 | #ifdef DEBUG_PYDICT |
| 462 | for (i=0; i < keys->dk_size; i++) { |
| 463 | Py_ssize_t ix = dk_get_index(keys, i); |
| 464 | assert(DKIX_DUMMY <= ix && ix <= usable); |
| 465 | } |
| 466 | |
| 467 | for (i=0; i < usable; i++) { |
| 468 | PyDictKeyEntry *entry = &entries[i]; |
| 469 | PyObject *key = entry->me_key; |
| 470 | |
| 471 | if (key != NULL) { |
| 472 | if (PyUnicode_CheckExact(key)) { |
| 473 | Py_hash_t hash = ((PyASCIIObject *)key)->hash; |
| 474 | assert(hash != -1); |
| 475 | assert(entry->me_hash == hash); |
| 476 | } |
| 477 | else { |
| 478 | /* test_dict fails if PyObject_Hash() is called again */ |
| 479 | assert(entry->me_hash != -1); |
| 480 | } |
| 481 | if (!splitted) { |
| 482 | assert(entry->me_value != NULL); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (splitted) { |
| 487 | assert(entry->me_value == NULL); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if (splitted) { |
| 492 | /* splitted table */ |
| 493 | for (i=0; i < mp->ma_used; i++) { |
| 494 | assert(mp->ma_values[i] != NULL); |
| 495 | } |
| 496 | } |
| 497 | #endif |
| 498 | |
| 499 | return 1; |
| 500 | } |
| 501 | #endif |
| 502 | |
| 503 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 504 | static PyDictKeysObject *new_keys_object(Py_ssize_t size) |
| 505 | { |
| 506 | PyDictKeysObject *dk; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 507 | Py_ssize_t es, usable; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 508 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 509 | assert(size >= PyDict_MINSIZE); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 510 | assert(IS_POWER_OF_2(size)); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 511 | |
| 512 | usable = USABLE_FRACTION(size); |
| 513 | if (size <= 0xff) { |
| 514 | es = 1; |
| 515 | } |
| 516 | else if (size <= 0xffff) { |
| 517 | es = 2; |
| 518 | } |
| 519 | #if SIZEOF_VOID_P > 4 |
| 520 | else if (size <= 0xffffffff) { |
| 521 | es = 4; |
| 522 | } |
| 523 | #endif |
| 524 | else { |
| 525 | es = sizeof(Py_ssize_t); |
| 526 | } |
| 527 | |
| 528 | if (size == PyDict_MINSIZE && numfreekeys > 0) { |
| 529 | dk = keys_free_list[--numfreekeys]; |
| 530 | } |
| 531 | else { |
Victor Stinner | 98ee9d5 | 2016-09-08 09:33:56 -0700 | [diff] [blame] | 532 | dk = PyObject_MALLOC(sizeof(PyDictKeysObject) |
Victor Stinner | 98ee9d5 | 2016-09-08 09:33:56 -0700 | [diff] [blame] | 533 | + es * size |
| 534 | + sizeof(PyDictKeyEntry) * usable); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 535 | if (dk == NULL) { |
| 536 | PyErr_NoMemory(); |
| 537 | return NULL; |
| 538 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 539 | } |
Antoine Pitrou | 2d169b2 | 2012-05-12 23:43:44 +0200 | [diff] [blame] | 540 | DK_DEBUG_INCREF dk->dk_refcnt = 1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 541 | dk->dk_size = size; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 542 | dk->dk_usable = usable; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 543 | dk->dk_lookup = lookdict_unicode_nodummy; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 544 | dk->dk_nentries = 0; |
Gregory P. Smith | 397f1b2 | 2018-04-19 22:41:19 -0700 | [diff] [blame] | 545 | memset(&dk->dk_indices[0], 0xff, es * size); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 546 | memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 547 | return dk; |
| 548 | } |
| 549 | |
| 550 | static void |
| 551 | free_keys_object(PyDictKeysObject *keys) |
| 552 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 553 | PyDictKeyEntry *entries = DK_ENTRIES(keys); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 554 | Py_ssize_t i, n; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 555 | for (i = 0, n = keys->dk_nentries; i < n; i++) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 556 | Py_XDECREF(entries[i].me_key); |
| 557 | Py_XDECREF(entries[i].me_value); |
| 558 | } |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 559 | if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) { |
| 560 | keys_free_list[numfreekeys++] = keys; |
| 561 | return; |
| 562 | } |
Raymond Hettinger | ce5179f | 2016-01-31 08:56:21 -0800 | [diff] [blame] | 563 | PyObject_FREE(keys); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | #define new_values(size) PyMem_NEW(PyObject *, size) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 567 | #define free_values(values) PyMem_FREE(values) |
| 568 | |
| 569 | /* Consumes a reference to the keys object */ |
| 570 | static PyObject * |
| 571 | new_dict(PyDictKeysObject *keys, PyObject **values) |
| 572 | { |
| 573 | PyDictObject *mp; |
Victor Stinner | c9b7f51 | 2013-07-08 22:19:20 +0200 | [diff] [blame] | 574 | assert(keys != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | if (numfree) { |
| 576 | mp = free_list[--numfree]; |
| 577 | assert (mp != NULL); |
| 578 | assert (Py_TYPE(mp) == &PyDict_Type); |
| 579 | _Py_NewReference((PyObject *)mp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 581 | else { |
| 582 | mp = PyObject_GC_New(PyDictObject, &PyDict_Type); |
| 583 | if (mp == NULL) { |
| 584 | DK_DECREF(keys); |
| 585 | free_values(values); |
| 586 | return NULL; |
| 587 | } |
| 588 | } |
| 589 | mp->ma_keys = keys; |
| 590 | mp->ma_values = values; |
| 591 | mp->ma_used = 0; |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 592 | mp->ma_version_tag = DICT_NEXT_VERSION(); |
Victor Stinner | 611b0fa | 2016-09-14 15:02:01 +0200 | [diff] [blame] | 593 | assert(_PyDict_CheckConsistency(mp)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | return (PyObject *)mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 597 | /* Consumes a reference to the keys object */ |
| 598 | static PyObject * |
| 599 | new_dict_with_shared_keys(PyDictKeysObject *keys) |
| 600 | { |
| 601 | PyObject **values; |
| 602 | Py_ssize_t i, size; |
| 603 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 604 | size = USABLE_FRACTION(DK_SIZE(keys)); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 605 | values = new_values(size); |
| 606 | if (values == NULL) { |
| 607 | DK_DECREF(keys); |
| 608 | return PyErr_NoMemory(); |
| 609 | } |
| 610 | for (i = 0; i < size; i++) { |
| 611 | values[i] = NULL; |
| 612 | } |
| 613 | return new_dict(keys, values); |
| 614 | } |
| 615 | |
Yury Selivanov | b0a7a03 | 2018-01-22 11:54:41 -0500 | [diff] [blame] | 616 | |
| 617 | static PyObject * |
| 618 | clone_combined_dict(PyDictObject *orig) |
| 619 | { |
| 620 | assert(PyDict_CheckExact(orig)); |
| 621 | assert(orig->ma_values == NULL); |
| 622 | assert(orig->ma_keys->dk_refcnt == 1); |
| 623 | |
| 624 | Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys); |
| 625 | PyDictKeysObject *keys = PyObject_Malloc(keys_size); |
| 626 | if (keys == NULL) { |
| 627 | PyErr_NoMemory(); |
| 628 | return NULL; |
| 629 | } |
| 630 | |
| 631 | memcpy(keys, orig->ma_keys, keys_size); |
| 632 | |
| 633 | /* After copying key/value pairs, we need to incref all |
| 634 | keys and values and they are about to be co-owned by a |
| 635 | new dict object. */ |
| 636 | PyDictKeyEntry *ep0 = DK_ENTRIES(keys); |
| 637 | Py_ssize_t n = keys->dk_nentries; |
| 638 | for (Py_ssize_t i = 0; i < n; i++) { |
| 639 | PyDictKeyEntry *entry = &ep0[i]; |
| 640 | PyObject *value = entry->me_value; |
| 641 | if (value != NULL) { |
| 642 | Py_INCREF(value); |
| 643 | Py_INCREF(entry->me_key); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | PyDictObject *new = (PyDictObject *)new_dict(keys, NULL); |
| 648 | if (new == NULL) { |
| 649 | /* In case of an error, `new_dict()` takes care of |
| 650 | cleaning up `keys`. */ |
| 651 | return NULL; |
| 652 | } |
| 653 | new->ma_used = orig->ma_used; |
| 654 | assert(_PyDict_CheckConsistency(new)); |
| 655 | if (_PyObject_GC_IS_TRACKED(orig)) { |
| 656 | /* Maintain tracking. */ |
| 657 | _PyObject_GC_TRACK(new); |
| 658 | } |
Yury Selivanov | 0b75228 | 2018-07-06 12:20:07 -0400 | [diff] [blame] | 659 | |
| 660 | /* Since we copied the keys table we now have an extra reference |
| 661 | in the system. Manually call _Py_INC_REFTOTAL to signal that |
| 662 | we have it now; calling DK_INCREF would be an error as |
| 663 | keys->dk_refcnt is already set to 1 (after memcpy). */ |
| 664 | _Py_INC_REFTOTAL; |
| 665 | |
Yury Selivanov | b0a7a03 | 2018-01-22 11:54:41 -0500 | [diff] [blame] | 666 | return (PyObject *)new; |
| 667 | } |
| 668 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 669 | PyObject * |
| 670 | PyDict_New(void) |
| 671 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 672 | PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE); |
Victor Stinner | c9b7f51 | 2013-07-08 22:19:20 +0200 | [diff] [blame] | 673 | if (keys == NULL) |
| 674 | return NULL; |
| 675 | return new_dict(keys, NULL); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 676 | } |
| 677 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 678 | /* Search index of hash table from offset of entry table */ |
| 679 | static Py_ssize_t |
| 680 | lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index) |
| 681 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 682 | size_t mask = DK_MASK(k); |
INADA Naoki | 073ae48 | 2017-06-23 15:22:50 +0900 | [diff] [blame] | 683 | size_t perturb = (size_t)hash; |
| 684 | size_t i = (size_t)hash & mask; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 685 | |
INADA Naoki | 073ae48 | 2017-06-23 15:22:50 +0900 | [diff] [blame] | 686 | for (;;) { |
| 687 | Py_ssize_t ix = dk_get_index(k, i); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 688 | if (ix == index) { |
| 689 | return i; |
| 690 | } |
| 691 | if (ix == DKIX_EMPTY) { |
| 692 | return DKIX_EMPTY; |
| 693 | } |
INADA Naoki | 073ae48 | 2017-06-23 15:22:50 +0900 | [diff] [blame] | 694 | perturb >>= PERTURB_SHIFT; |
| 695 | i = mask & (i*5 + perturb + 1); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 696 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 697 | Py_UNREACHABLE(); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 700 | /* |
| 701 | The basic lookup function used by all operations. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 702 | This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 703 | Open addressing is preferred over chaining since the link overhead for |
| 704 | chaining would be substantial (100% with typical malloc overhead). |
| 705 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 706 | The initial probe index is computed as hash mod the table size. Subsequent |
| 707 | probe indices are computed as explained earlier. |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 708 | |
| 709 | All arithmetic on hash should ignore overflow. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 710 | |
Guido van Rossum | dc5f6b2 | 2006-08-24 21:29:26 +0000 | [diff] [blame] | 711 | The details in this version are due to Tim Peters, building on many past |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 712 | contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and |
Guido van Rossum | dc5f6b2 | 2006-08-24 21:29:26 +0000 | [diff] [blame] | 713 | Christian Tismer. |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 714 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 715 | lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a |
Victor Stinner | a4348cc | 2016-09-08 12:01:25 -0700 | [diff] [blame] | 716 | comparison raises an exception. |
Guido van Rossum | 89d8c60 | 2007-09-18 17:26:56 +0000 | [diff] [blame] | 717 | lookdict_unicode() below is specialized to string keys, comparison of which can |
INADA Naoki | 1b8df10 | 2017-02-20 22:48:10 +0900 | [diff] [blame] | 718 | never raise an exception; that function can never return DKIX_ERROR when key |
| 719 | is string. Otherwise, it falls back to lookdict(). |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 720 | lookdict_unicode_nodummy is further specialized for string keys that cannot be |
| 721 | the <dummy> value. |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 722 | For both, when the key isn't found a DKIX_EMPTY is returned. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 723 | */ |
Victor Stinner | c7a8f67 | 2016-11-15 15:13:40 +0100 | [diff] [blame] | 724 | static Py_ssize_t _Py_HOT_FUNCTION |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 725 | lookdict(PyDictObject *mp, PyObject *key, |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 726 | Py_hash_t hash, PyObject **value_addr) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 727 | { |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 728 | size_t i, mask, perturb; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 729 | PyDictKeysObject *dk; |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 730 | PyDictKeyEntry *ep0; |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 731 | |
Antoine Pitrou | 9a23490 | 2012-05-13 20:48:01 +0200 | [diff] [blame] | 732 | top: |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 733 | dk = mp->ma_keys; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 734 | ep0 = DK_ENTRIES(dk); |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 735 | mask = DK_MASK(dk); |
| 736 | perturb = hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 737 | i = (size_t)hash & mask; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 738 | |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 739 | for (;;) { |
| 740 | Py_ssize_t ix = dk_get_index(dk, i); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 741 | if (ix == DKIX_EMPTY) { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 742 | *value_addr = NULL; |
| 743 | return ix; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 744 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 745 | if (ix >= 0) { |
| 746 | PyDictKeyEntry *ep = &ep0[ix]; |
| 747 | assert(ep->me_key != NULL); |
| 748 | if (ep->me_key == key) { |
| 749 | *value_addr = ep->me_value; |
| 750 | return ix; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 751 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 752 | if (ep->me_hash == hash) { |
| 753 | PyObject *startkey = ep->me_key; |
| 754 | Py_INCREF(startkey); |
| 755 | int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
| 756 | Py_DECREF(startkey); |
| 757 | if (cmp < 0) { |
| 758 | *value_addr = NULL; |
| 759 | return DKIX_ERROR; |
| 760 | } |
| 761 | if (dk == mp->ma_keys && ep->me_key == startkey) { |
| 762 | if (cmp > 0) { |
| 763 | *value_addr = ep->me_value; |
| 764 | return ix; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 765 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 766 | } |
| 767 | else { |
| 768 | /* The dict was mutated, restart */ |
| 769 | goto top; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 770 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 772 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 773 | perturb >>= PERTURB_SHIFT; |
| 774 | i = (i*5 + perturb + 1) & mask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 776 | Py_UNREACHABLE(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 779 | /* Specialized version for string-only keys */ |
Victor Stinner | c7a8f67 | 2016-11-15 15:13:40 +0100 | [diff] [blame] | 780 | static Py_ssize_t _Py_HOT_FUNCTION |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 781 | lookdict_unicode(PyDictObject *mp, PyObject *key, |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 782 | Py_hash_t hash, PyObject **value_addr) |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 783 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 784 | assert(mp->ma_values == NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | /* Make sure this function doesn't have to handle non-unicode keys, |
| 786 | including subclasses of str; e.g., one reason to subclass |
| 787 | unicodes is to override __eq__, and for speed we don't cater to |
| 788 | that here. */ |
| 789 | if (!PyUnicode_CheckExact(key)) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 790 | mp->ma_keys->dk_lookup = lookdict; |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 791 | return lookdict(mp, key, hash, value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 792 | } |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 793 | |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 794 | PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys); |
| 795 | size_t mask = DK_MASK(mp->ma_keys); |
| 796 | size_t perturb = (size_t)hash; |
| 797 | size_t i = (size_t)hash & mask; |
| 798 | |
| 799 | for (;;) { |
| 800 | Py_ssize_t ix = dk_get_index(mp->ma_keys, i); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 801 | if (ix == DKIX_EMPTY) { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 802 | *value_addr = NULL; |
| 803 | return DKIX_EMPTY; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 804 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 805 | if (ix >= 0) { |
| 806 | PyDictKeyEntry *ep = &ep0[ix]; |
| 807 | assert(ep->me_key != NULL); |
| 808 | assert(PyUnicode_CheckExact(ep->me_key)); |
| 809 | if (ep->me_key == key || |
| 810 | (ep->me_hash == hash && unicode_eq(ep->me_key, key))) { |
| 811 | *value_addr = ep->me_value; |
| 812 | return ix; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 813 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 814 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 815 | perturb >>= PERTURB_SHIFT; |
| 816 | i = mask & (i*5 + perturb + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 818 | Py_UNREACHABLE(); |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 821 | /* Faster version of lookdict_unicode when it is known that no <dummy> keys |
| 822 | * will be present. */ |
Victor Stinner | c7a8f67 | 2016-11-15 15:13:40 +0100 | [diff] [blame] | 823 | static Py_ssize_t _Py_HOT_FUNCTION |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 824 | lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key, |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 825 | Py_hash_t hash, PyObject **value_addr) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 826 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 827 | assert(mp->ma_values == NULL); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 828 | /* Make sure this function doesn't have to handle non-unicode keys, |
| 829 | including subclasses of str; e.g., one reason to subclass |
| 830 | unicodes is to override __eq__, and for speed we don't cater to |
| 831 | that here. */ |
| 832 | if (!PyUnicode_CheckExact(key)) { |
| 833 | mp->ma_keys->dk_lookup = lookdict; |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 834 | return lookdict(mp, key, hash, value_addr); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 835 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 836 | |
| 837 | PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys); |
| 838 | size_t mask = DK_MASK(mp->ma_keys); |
| 839 | size_t perturb = (size_t)hash; |
| 840 | size_t i = (size_t)hash & mask; |
| 841 | |
| 842 | for (;;) { |
| 843 | Py_ssize_t ix = dk_get_index(mp->ma_keys, i); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 844 | assert (ix != DKIX_DUMMY); |
| 845 | if (ix == DKIX_EMPTY) { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 846 | *value_addr = NULL; |
| 847 | return DKIX_EMPTY; |
| 848 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 849 | PyDictKeyEntry *ep = &ep0[ix]; |
| 850 | assert(ep->me_key != NULL); |
| 851 | assert(PyUnicode_CheckExact(ep->me_key)); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 852 | if (ep->me_key == key || |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 853 | (ep->me_hash == hash && unicode_eq(ep->me_key, key))) { |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 854 | *value_addr = ep->me_value; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 855 | return ix; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 856 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 857 | perturb >>= PERTURB_SHIFT; |
| 858 | i = mask & (i*5 + perturb + 1); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 859 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 860 | Py_UNREACHABLE(); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /* Version of lookdict for split tables. |
| 864 | * All split tables and only split tables use this lookup function. |
| 865 | * Split tables only contain unicode keys and no dummy keys, |
| 866 | * so algorithm is the same as lookdict_unicode_nodummy. |
| 867 | */ |
Victor Stinner | c7a8f67 | 2016-11-15 15:13:40 +0100 | [diff] [blame] | 868 | static Py_ssize_t _Py_HOT_FUNCTION |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 869 | lookdict_split(PyDictObject *mp, PyObject *key, |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 870 | Py_hash_t hash, PyObject **value_addr) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 871 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 872 | /* mp must split table */ |
| 873 | assert(mp->ma_values != NULL); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 874 | if (!PyUnicode_CheckExact(key)) { |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 875 | Py_ssize_t ix = lookdict(mp, key, hash, value_addr); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 876 | if (ix >= 0) { |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 877 | *value_addr = mp->ma_values[ix]; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 878 | } |
| 879 | return ix; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 880 | } |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 881 | |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 882 | PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys); |
| 883 | size_t mask = DK_MASK(mp->ma_keys); |
| 884 | size_t perturb = (size_t)hash; |
| 885 | size_t i = (size_t)hash & mask; |
| 886 | |
| 887 | for (;;) { |
| 888 | Py_ssize_t ix = dk_get_index(mp->ma_keys, i); |
| 889 | assert (ix != DKIX_DUMMY); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 890 | if (ix == DKIX_EMPTY) { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 891 | *value_addr = NULL; |
| 892 | return DKIX_EMPTY; |
| 893 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 894 | PyDictKeyEntry *ep = &ep0[ix]; |
| 895 | assert(ep->me_key != NULL); |
| 896 | assert(PyUnicode_CheckExact(ep->me_key)); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 897 | if (ep->me_key == key || |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 898 | (ep->me_hash == hash && unicode_eq(ep->me_key, key))) { |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 899 | *value_addr = mp->ma_values[ix]; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 900 | return ix; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 901 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 902 | perturb >>= PERTURB_SHIFT; |
| 903 | i = mask & (i*5 + perturb + 1); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 904 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 905 | Py_UNREACHABLE(); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 906 | } |
| 907 | |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 908 | int |
| 909 | _PyDict_HasOnlyStringKeys(PyObject *dict) |
| 910 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | Py_ssize_t pos = 0; |
| 912 | PyObject *key, *value; |
Benjamin Peterson | f609654 | 2010-11-17 22:33:12 +0000 | [diff] [blame] | 913 | assert(PyDict_Check(dict)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | /* Shortcut */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 915 | if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | return 1; |
| 917 | while (PyDict_Next(dict, &pos, &key, &value)) |
| 918 | if (!PyUnicode_Check(key)) |
| 919 | return 0; |
| 920 | return 1; |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 923 | #define MAINTAIN_TRACKING(mp, key, value) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | do { \ |
| 925 | if (!_PyObject_GC_IS_TRACKED(mp)) { \ |
| 926 | if (_PyObject_GC_MAY_BE_TRACKED(key) || \ |
| 927 | _PyObject_GC_MAY_BE_TRACKED(value)) { \ |
| 928 | _PyObject_GC_TRACK(mp); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | } \ |
| 930 | } \ |
| 931 | } while(0) |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 932 | |
| 933 | void |
| 934 | _PyDict_MaybeUntrack(PyObject *op) |
| 935 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | PyDictObject *mp; |
| 937 | PyObject *value; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 938 | Py_ssize_t i, numentries; |
| 939 | PyDictKeyEntry *ep0; |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op)) |
| 942 | return; |
| 943 | |
| 944 | mp = (PyDictObject *) op; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 945 | ep0 = DK_ENTRIES(mp->ma_keys); |
| 946 | numentries = mp->ma_keys->dk_nentries; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 947 | if (_PyDict_HasSplitTable(mp)) { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 948 | for (i = 0; i < numentries; i++) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 949 | if ((value = mp->ma_values[i]) == NULL) |
| 950 | continue; |
| 951 | if (_PyObject_GC_MAY_BE_TRACKED(value)) { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 952 | assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key)); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 953 | return; |
| 954 | } |
| 955 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 957 | else { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 958 | for (i = 0; i < numentries; i++) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 959 | if ((value = ep0[i].me_value) == NULL) |
| 960 | continue; |
| 961 | if (_PyObject_GC_MAY_BE_TRACKED(value) || |
| 962 | _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key)) |
| 963 | return; |
| 964 | } |
| 965 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | _PyObject_GC_UNTRACK(op); |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 969 | /* Internal function to find slot for an item from its hash |
Victor Stinner | 3c336c5 | 2016-09-12 14:17:40 +0200 | [diff] [blame] | 970 | when it is known that the key is not present in the dict. |
| 971 | |
| 972 | The dict must be combined. */ |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 973 | static Py_ssize_t |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 974 | find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 975 | { |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 976 | assert(keys != NULL); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 977 | |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 978 | const size_t mask = DK_MASK(keys); |
| 979 | size_t i = hash & mask; |
| 980 | Py_ssize_t ix = dk_get_index(keys, i); |
| 981 | for (size_t perturb = hash; ix >= 0;) { |
INADA Naoki | 267941c | 2016-10-06 15:19:07 +0900 | [diff] [blame] | 982 | perturb >>= PERTURB_SHIFT; |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 983 | i = (i*5 + perturb + 1) & mask; |
| 984 | ix = dk_get_index(keys, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 985 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 986 | return i; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 989 | static int |
| 990 | insertion_resize(PyDictObject *mp) |
| 991 | { |
Raymond Hettinger | 36f74aa | 2013-05-17 03:01:13 -0700 | [diff] [blame] | 992 | return dictresize(mp, GROWTH_RATE(mp)); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 993 | } |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 994 | |
| 995 | /* |
| 996 | Internal routine to insert a new item into the table. |
| 997 | Used both by the internal resize routine and by the public insert routine. |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 998 | Returns -1 if an error occurred, or 0 on success. |
| 999 | */ |
| 1000 | static int |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1001 | insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value) |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1002 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1003 | PyObject *old_value; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1004 | PyDictKeyEntry *ep; |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1005 | |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 1006 | Py_INCREF(key); |
| 1007 | Py_INCREF(value); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1008 | if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) { |
| 1009 | if (insertion_resize(mp) < 0) |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 1010 | goto Fail; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1011 | } |
| 1012 | |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1013 | Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value); |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 1014 | if (ix == DKIX_ERROR) |
| 1015 | goto Fail; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1016 | |
Antoine Pitrou | d696732 | 2014-10-18 00:35:00 +0200 | [diff] [blame] | 1017 | assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1018 | MAINTAIN_TRACKING(mp, key, value); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1019 | |
| 1020 | /* When insertion order is different from shared key, we can't share |
| 1021 | * the key anymore. Convert this instance to combine table. |
| 1022 | */ |
| 1023 | if (_PyDict_HasSplitTable(mp) && |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1024 | ((ix >= 0 && old_value == NULL && mp->ma_used != ix) || |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1025 | (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) { |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 1026 | if (insertion_resize(mp) < 0) |
| 1027 | goto Fail; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1028 | ix = DKIX_EMPTY; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1029 | } |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1030 | |
| 1031 | if (ix == DKIX_EMPTY) { |
| 1032 | /* Insert into new slot. */ |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1033 | assert(old_value == NULL); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1034 | if (mp->ma_keys->dk_usable <= 0) { |
| 1035 | /* Need to resize. */ |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 1036 | if (insertion_resize(mp) < 0) |
| 1037 | goto Fail; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1038 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1039 | Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash); |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1040 | ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries]; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1041 | dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1042 | ep->me_key = key; |
| 1043 | ep->me_hash = hash; |
| 1044 | if (mp->ma_values) { |
| 1045 | assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL); |
| 1046 | mp->ma_values[mp->ma_keys->dk_nentries] = value; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1047 | } |
| 1048 | else { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1049 | ep->me_value = value; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1050 | } |
| 1051 | mp->ma_used++; |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 1052 | mp->ma_version_tag = DICT_NEXT_VERSION(); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1053 | mp->ma_keys->dk_usable--; |
| 1054 | mp->ma_keys->dk_nentries++; |
| 1055 | assert(mp->ma_keys->dk_usable >= 0); |
Victor Stinner | 611b0fa | 2016-09-14 15:02:01 +0200 | [diff] [blame] | 1056 | assert(_PyDict_CheckConsistency(mp)); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1057 | return 0; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1058 | } |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1059 | |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1060 | if (_PyDict_HasSplitTable(mp)) { |
| 1061 | mp->ma_values[ix] = value; |
| 1062 | if (old_value == NULL) { |
| 1063 | /* pending state */ |
| 1064 | assert(ix == mp->ma_used); |
| 1065 | mp->ma_used++; |
| 1066 | } |
| 1067 | } |
| 1068 | else { |
| 1069 | assert(old_value != NULL); |
| 1070 | DK_ENTRIES(mp->ma_keys)[ix].me_value = value; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 1073 | mp->ma_version_tag = DICT_NEXT_VERSION(); |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1074 | Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */ |
Victor Stinner | 611b0fa | 2016-09-14 15:02:01 +0200 | [diff] [blame] | 1075 | assert(_PyDict_CheckConsistency(mp)); |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 1076 | Py_DECREF(key); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1077 | return 0; |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 1078 | |
| 1079 | Fail: |
| 1080 | Py_DECREF(value); |
| 1081 | Py_DECREF(key); |
| 1082 | return -1; |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1083 | } |
| 1084 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1085 | /* |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 1086 | Internal routine used by dictresize() to build a hashtable of entries. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1087 | */ |
| 1088 | static void |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1089 | build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1090 | { |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1091 | size_t mask = (size_t)DK_SIZE(keys) - 1; |
| 1092 | for (Py_ssize_t ix = 0; ix != n; ix++, ep++) { |
| 1093 | Py_hash_t hash = ep->me_hash; |
| 1094 | size_t i = hash & mask; |
| 1095 | for (size_t perturb = hash; dk_get_index(keys, i) != DKIX_EMPTY;) { |
| 1096 | perturb >>= PERTURB_SHIFT; |
INADA Naoki | 870c286 | 2017-06-24 09:03:19 +0900 | [diff] [blame] | 1097 | i = mask & (i*5 + perturb + 1); |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1098 | } |
| 1099 | dk_set_index(keys, i, ix); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | /* |
| 1104 | Restructure the table by allocating a new table and reinserting all |
| 1105 | items again. When entries have been deleted, the new table may |
| 1106 | actually be smaller than the old one. |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1107 | If a table is split (its keys and hashes are shared, its values are not), |
| 1108 | then the values are temporarily copied into the table, it is resized as |
| 1109 | a combined table, then the me_value slots in the old table are NULLed out. |
| 1110 | After resizing a table is always combined, |
| 1111 | but can be resplit by make_keys_shared(). |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1112 | */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1113 | static int |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 1114 | dictresize(PyDictObject *mp, Py_ssize_t minsize) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1115 | { |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1116 | Py_ssize_t newsize, numentries; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1117 | PyDictKeysObject *oldkeys; |
| 1118 | PyObject **oldvalues; |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1119 | PyDictKeyEntry *oldentries, *newentries; |
Tim Peters | 91a364d | 2001-05-19 07:04:38 +0000 | [diff] [blame] | 1120 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1121 | /* Find the smallest table size > minused. */ |
| 1122 | for (newsize = PyDict_MINSIZE; |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 1123 | newsize < minsize && newsize > 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | newsize <<= 1) |
| 1125 | ; |
| 1126 | if (newsize <= 0) { |
| 1127 | PyErr_NoMemory(); |
| 1128 | return -1; |
| 1129 | } |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1130 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1131 | oldkeys = mp->ma_keys; |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1132 | |
| 1133 | /* NOTE: Current odict checks mp->ma_keys to detect resize happen. |
| 1134 | * So we can't reuse oldkeys even if oldkeys->dk_size == newsize. |
| 1135 | * TODO: Try reusing oldkeys when reimplement odict. |
| 1136 | */ |
| 1137 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1138 | /* Allocate a new table. */ |
| 1139 | mp->ma_keys = new_keys_object(newsize); |
| 1140 | if (mp->ma_keys == NULL) { |
| 1141 | mp->ma_keys = oldkeys; |
| 1142 | return -1; |
| 1143 | } |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 1144 | // New table must be large enough. |
| 1145 | assert(mp->ma_keys->dk_usable >= mp->ma_used); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1146 | if (oldkeys->dk_lookup == lookdict) |
| 1147 | mp->ma_keys->dk_lookup = lookdict; |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1148 | |
| 1149 | numentries = mp->ma_used; |
| 1150 | oldentries = DK_ENTRIES(oldkeys); |
| 1151 | newentries = DK_ENTRIES(mp->ma_keys); |
| 1152 | oldvalues = mp->ma_values; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1153 | if (oldvalues != NULL) { |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1154 | /* Convert split table into new combined table. |
| 1155 | * We must incref keys; we can transfer values. |
| 1156 | * Note that values of split table is always dense. |
| 1157 | */ |
| 1158 | for (Py_ssize_t i = 0; i < numentries; i++) { |
| 1159 | assert(oldvalues[i] != NULL); |
| 1160 | PyDictKeyEntry *ep = &oldentries[i]; |
| 1161 | PyObject *key = ep->me_key; |
| 1162 | Py_INCREF(key); |
| 1163 | newentries[i].me_key = key; |
| 1164 | newentries[i].me_hash = ep->me_hash; |
| 1165 | newentries[i].me_value = oldvalues[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1166 | } |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1167 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1168 | DK_DECREF(oldkeys); |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1169 | mp->ma_values = NULL; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1170 | if (oldvalues != empty_values) { |
| 1171 | free_values(oldvalues); |
| 1172 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1173 | } |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1174 | else { // combined table. |
| 1175 | if (oldkeys->dk_nentries == numentries) { |
| 1176 | memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry)); |
| 1177 | } |
| 1178 | else { |
| 1179 | PyDictKeyEntry *ep = oldentries; |
| 1180 | for (Py_ssize_t i = 0; i < numentries; i++) { |
| 1181 | while (ep->me_value == NULL) |
| 1182 | ep++; |
| 1183 | newentries[i] = *ep++; |
| 1184 | } |
| 1185 | } |
| 1186 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1187 | assert(oldkeys->dk_lookup != lookdict_split); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1188 | assert(oldkeys->dk_refcnt == 1); |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1189 | if (oldkeys->dk_size == PyDict_MINSIZE && |
| 1190 | numfreekeys < PyDict_MAXFREELIST) { |
| 1191 | DK_DEBUG_DECREF keys_free_list[numfreekeys++] = oldkeys; |
| 1192 | } |
| 1193 | else { |
| 1194 | DK_DEBUG_DECREF PyObject_FREE(oldkeys); |
| 1195 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1196 | } |
Serhiy Storchaka | e26e20d | 2016-10-29 10:50:00 +0300 | [diff] [blame] | 1197 | |
| 1198 | build_indices(mp->ma_keys, newentries, numentries); |
| 1199 | mp->ma_keys->dk_usable -= numentries; |
| 1200 | mp->ma_keys->dk_nentries = numentries; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | return 0; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Benjamin Peterson | 15ee821 | 2012-04-24 14:44:18 -0400 | [diff] [blame] | 1204 | /* Returns NULL if unable to split table. |
| 1205 | * A NULL return does not necessarily indicate an error */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1206 | static PyDictKeysObject * |
| 1207 | make_keys_shared(PyObject *op) |
| 1208 | { |
| 1209 | Py_ssize_t i; |
| 1210 | Py_ssize_t size; |
| 1211 | PyDictObject *mp = (PyDictObject *)op; |
| 1212 | |
Benjamin Peterson | 15ee821 | 2012-04-24 14:44:18 -0400 | [diff] [blame] | 1213 | if (!PyDict_CheckExact(op)) |
| 1214 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1215 | if (!_PyDict_HasSplitTable(mp)) { |
| 1216 | PyDictKeyEntry *ep0; |
| 1217 | PyObject **values; |
| 1218 | assert(mp->ma_keys->dk_refcnt == 1); |
| 1219 | if (mp->ma_keys->dk_lookup == lookdict) { |
| 1220 | return NULL; |
| 1221 | } |
| 1222 | else if (mp->ma_keys->dk_lookup == lookdict_unicode) { |
| 1223 | /* Remove dummy keys */ |
| 1224 | if (dictresize(mp, DK_SIZE(mp->ma_keys))) |
| 1225 | return NULL; |
| 1226 | } |
| 1227 | assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy); |
| 1228 | /* Copy values into a new array */ |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1229 | ep0 = DK_ENTRIES(mp->ma_keys); |
| 1230 | size = USABLE_FRACTION(DK_SIZE(mp->ma_keys)); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1231 | values = new_values(size); |
| 1232 | if (values == NULL) { |
| 1233 | PyErr_SetString(PyExc_MemoryError, |
| 1234 | "Not enough memory to allocate new values array"); |
| 1235 | return NULL; |
| 1236 | } |
| 1237 | for (i = 0; i < size; i++) { |
| 1238 | values[i] = ep0[i].me_value; |
| 1239 | ep0[i].me_value = NULL; |
| 1240 | } |
| 1241 | mp->ma_keys->dk_lookup = lookdict_split; |
| 1242 | mp->ma_values = values; |
| 1243 | } |
| 1244 | DK_INCREF(mp->ma_keys); |
| 1245 | return mp->ma_keys; |
| 1246 | } |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 1247 | |
| 1248 | PyObject * |
| 1249 | _PyDict_NewPresized(Py_ssize_t minused) |
| 1250 | { |
INADA Naoki | 92c50ee | 2016-11-22 00:57:02 +0900 | [diff] [blame] | 1251 | const Py_ssize_t max_presize = 128 * 1024; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1252 | Py_ssize_t newsize; |
| 1253 | PyDictKeysObject *new_keys; |
INADA Naoki | 92c50ee | 2016-11-22 00:57:02 +0900 | [diff] [blame] | 1254 | |
| 1255 | /* There are no strict guarantee that returned dict can contain minused |
| 1256 | * items without resize. So we create medium size dict instead of very |
| 1257 | * large dict or MemoryError. |
| 1258 | */ |
| 1259 | if (minused > USABLE_FRACTION(max_presize)) { |
| 1260 | newsize = max_presize; |
| 1261 | } |
| 1262 | else { |
| 1263 | Py_ssize_t minsize = ESTIMATE_SIZE(minused); |
| 1264 | newsize = PyDict_MINSIZE; |
| 1265 | while (newsize < minsize) { |
| 1266 | newsize <<= 1; |
| 1267 | } |
| 1268 | } |
| 1269 | assert(IS_POWER_OF_2(newsize)); |
| 1270 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1271 | new_keys = new_keys_object(newsize); |
| 1272 | if (new_keys == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1273 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1274 | return new_dict(new_keys, NULL); |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1277 | /* Note that, for historical reasons, PyDict_GetItem() suppresses all errors |
| 1278 | * that may occur (originally dicts supported only string keys, and exceptions |
| 1279 | * weren't possible). So, while the original intent was that a NULL return |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1280 | * meant the key wasn't present, in reality it can mean that, or that an error |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1281 | * (suppressed) occurred while computing the key's hash, or that some error |
| 1282 | * (suppressed) occurred when comparing keys in the dict's internal probe |
| 1283 | * sequence. A nasty example of the latter is when a Python-coded comparison |
| 1284 | * function hits a stack-depth error, which can cause this to return NULL |
| 1285 | * even if the key is present. |
| 1286 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1287 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1288 | PyDict_GetItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1289 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1290 | Py_hash_t hash; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1291 | Py_ssize_t ix; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | PyDictObject *mp = (PyDictObject *)op; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | PyThreadState *tstate; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1294 | PyObject *value; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | if (!PyDict_Check(op)) |
| 1297 | return NULL; |
| 1298 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1299 | (hash = ((PyASCIIObject *) key)->hash) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1300 | { |
| 1301 | hash = PyObject_Hash(key); |
| 1302 | if (hash == -1) { |
| 1303 | PyErr_Clear(); |
| 1304 | return NULL; |
| 1305 | } |
| 1306 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1308 | /* We can arrive here with a NULL tstate during initialization: try |
| 1309 | running "python -Wi" for an example related to string interning. |
| 1310 | Let's just hope that no exception occurs then... This must be |
| 1311 | _PyThreadState_Current and not PyThreadState_GET() because in debug |
| 1312 | mode, the latter complains if tstate is NULL. */ |
Victor Stinner | 0cae609 | 2016-11-11 01:43:56 +0100 | [diff] [blame] | 1313 | tstate = PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1314 | if (tstate != NULL && tstate->curexc_type != NULL) { |
| 1315 | /* preserve the existing exception */ |
| 1316 | PyObject *err_type, *err_value, *err_tb; |
| 1317 | PyErr_Fetch(&err_type, &err_value, &err_tb); |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1318 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1319 | /* ignore errors */ |
| 1320 | PyErr_Restore(err_type, err_value, err_tb); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1321 | if (ix < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | return NULL; |
| 1323 | } |
| 1324 | else { |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1325 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1326 | if (ix < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | PyErr_Clear(); |
| 1328 | return NULL; |
| 1329 | } |
| 1330 | } |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1331 | return value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 1334 | /* Same as PyDict_GetItemWithError() but with hash supplied by caller. |
| 1335 | This returns NULL *with* an exception set if an exception occurred. |
| 1336 | It returns NULL *without* an exception set if the key wasn't present. |
| 1337 | */ |
Raymond Hettinger | 4b74fba | 2014-05-03 16:32:11 -0700 | [diff] [blame] | 1338 | PyObject * |
| 1339 | _PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash) |
| 1340 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1341 | Py_ssize_t ix; |
Raymond Hettinger | 4b74fba | 2014-05-03 16:32:11 -0700 | [diff] [blame] | 1342 | PyDictObject *mp = (PyDictObject *)op; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1343 | PyObject *value; |
Raymond Hettinger | 4b74fba | 2014-05-03 16:32:11 -0700 | [diff] [blame] | 1344 | |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 1345 | if (!PyDict_Check(op)) { |
| 1346 | PyErr_BadInternalCall(); |
Raymond Hettinger | 4b74fba | 2014-05-03 16:32:11 -0700 | [diff] [blame] | 1347 | return NULL; |
Raymond Hettinger | 4b74fba | 2014-05-03 16:32:11 -0700 | [diff] [blame] | 1348 | } |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 1349 | |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1350 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value); |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 1351 | if (ix < 0) { |
| 1352 | return NULL; |
Raymond Hettinger | 4b74fba | 2014-05-03 16:32:11 -0700 | [diff] [blame] | 1353 | } |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1354 | return value; |
Raymond Hettinger | 4b74fba | 2014-05-03 16:32:11 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1357 | /* Variant of PyDict_GetItem() that doesn't suppress exceptions. |
| 1358 | This returns NULL *with* an exception set if an exception occurred. |
| 1359 | It returns NULL *without* an exception set if the key wasn't present. |
| 1360 | */ |
| 1361 | PyObject * |
| 1362 | PyDict_GetItemWithError(PyObject *op, PyObject *key) |
| 1363 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1364 | Py_ssize_t ix; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1365 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | PyDictObject*mp = (PyDictObject *)op; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1367 | PyObject *value; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | if (!PyDict_Check(op)) { |
| 1370 | PyErr_BadInternalCall(); |
| 1371 | return NULL; |
| 1372 | } |
| 1373 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1374 | (hash = ((PyASCIIObject *) key)->hash) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | { |
| 1376 | hash = PyObject_Hash(key); |
| 1377 | if (hash == -1) { |
| 1378 | return NULL; |
| 1379 | } |
| 1380 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1381 | |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1382 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1383 | if (ix < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1384 | return NULL; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1385 | return value; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1388 | PyObject * |
| 1389 | _PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key) |
| 1390 | { |
| 1391 | PyObject *kv; |
| 1392 | kv = _PyUnicode_FromId(key); /* borrowed */ |
| 1393 | if (kv == NULL) |
| 1394 | return NULL; |
| 1395 | return PyDict_GetItemWithError(dp, kv); |
| 1396 | } |
| 1397 | |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 1398 | /* Fast version of global value lookup (LOAD_GLOBAL). |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1399 | * Lookup in globals, then builtins. |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 1400 | * |
| 1401 | * Raise an exception and return NULL if an error occurred (ex: computing the |
| 1402 | * key hash failed, key comparison failed, ...). Return NULL if the key doesn't |
| 1403 | * exist. Return the value if the key exists. |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1404 | */ |
| 1405 | PyObject * |
| 1406 | _PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1407 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1408 | Py_ssize_t ix; |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 1409 | Py_hash_t hash; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1410 | PyObject *value; |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 1411 | |
| 1412 | if (!PyUnicode_CheckExact(key) || |
| 1413 | (hash = ((PyASCIIObject *) key)->hash) == -1) |
| 1414 | { |
| 1415 | hash = PyObject_Hash(key); |
| 1416 | if (hash == -1) |
| 1417 | return NULL; |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1418 | } |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 1419 | |
| 1420 | /* namespace 1: globals */ |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1421 | ix = globals->ma_keys->dk_lookup(globals, key, hash, &value); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1422 | if (ix == DKIX_ERROR) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1423 | return NULL; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1424 | if (ix != DKIX_EMPTY && value != NULL) |
| 1425 | return value; |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 1426 | |
| 1427 | /* namespace 2: builtins */ |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1428 | ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1429 | if (ix < 0) |
Victor Stinner | b4efc96 | 2015-11-20 09:24:02 +0100 | [diff] [blame] | 1430 | return NULL; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1431 | return value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1434 | /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the |
| 1435 | * dictionary if it's merely replacing the value for an existing key. |
| 1436 | * This means that it's safe to loop over a dictionary with PyDict_Next() |
| 1437 | * and occasionally replace a value -- but you can't insert new keys or |
| 1438 | * remove them. |
| 1439 | */ |
| 1440 | int |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1441 | PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value) |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1442 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1443 | PyDictObject *mp; |
| 1444 | Py_hash_t hash; |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1445 | if (!PyDict_Check(op)) { |
| 1446 | PyErr_BadInternalCall(); |
| 1447 | return -1; |
| 1448 | } |
| 1449 | assert(key); |
| 1450 | assert(value); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1451 | mp = (PyDictObject *)op; |
| 1452 | if (!PyUnicode_CheckExact(key) || |
| 1453 | (hash = ((PyASCIIObject *) key)->hash) == -1) |
| 1454 | { |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1455 | hash = PyObject_Hash(key); |
| 1456 | if (hash == -1) |
| 1457 | return -1; |
| 1458 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1459 | |
| 1460 | /* insertdict() handles any resizing that might be necessary */ |
| 1461 | return insertdict(mp, key, hash, value); |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1462 | } |
| 1463 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1464 | int |
Raymond Hettinger | 4b74fba | 2014-05-03 16:32:11 -0700 | [diff] [blame] | 1465 | _PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value, |
| 1466 | Py_hash_t hash) |
| 1467 | { |
| 1468 | PyDictObject *mp; |
| 1469 | |
| 1470 | if (!PyDict_Check(op)) { |
| 1471 | PyErr_BadInternalCall(); |
| 1472 | return -1; |
| 1473 | } |
| 1474 | assert(key); |
| 1475 | assert(value); |
Serhiy Storchaka | b9d98d5 | 2015-10-02 12:47:11 +0300 | [diff] [blame] | 1476 | assert(hash != -1); |
Raymond Hettinger | 4b74fba | 2014-05-03 16:32:11 -0700 | [diff] [blame] | 1477 | mp = (PyDictObject *)op; |
| 1478 | |
| 1479 | /* insertdict() handles any resizing that might be necessary */ |
| 1480 | return insertdict(mp, key, hash, value); |
| 1481 | } |
| 1482 | |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1483 | static int |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1484 | delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix, |
Antoine Pitrou | c06ae20 | 2016-12-27 14:34:54 +0100 | [diff] [blame] | 1485 | PyObject *old_value) |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1486 | { |
Antoine Pitrou | c06ae20 | 2016-12-27 14:34:54 +0100 | [diff] [blame] | 1487 | PyObject *old_key; |
Antoine Pitrou | d741ed4 | 2016-12-27 14:23:43 +0100 | [diff] [blame] | 1488 | PyDictKeyEntry *ep; |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1489 | |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1490 | Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix); |
| 1491 | assert(hashpos >= 0); |
| 1492 | |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1493 | mp->ma_used--; |
Antoine Pitrou | d741ed4 | 2016-12-27 14:23:43 +0100 | [diff] [blame] | 1494 | mp->ma_version_tag = DICT_NEXT_VERSION(); |
| 1495 | ep = &DK_ENTRIES(mp->ma_keys)[ix]; |
| 1496 | dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY); |
| 1497 | ENSURE_ALLOWS_DELETIONS(mp); |
| 1498 | old_key = ep->me_key; |
| 1499 | ep->me_key = NULL; |
Antoine Pitrou | c06ae20 | 2016-12-27 14:34:54 +0100 | [diff] [blame] | 1500 | ep->me_value = NULL; |
Antoine Pitrou | d741ed4 | 2016-12-27 14:23:43 +0100 | [diff] [blame] | 1501 | Py_DECREF(old_key); |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1502 | Py_DECREF(old_value); |
Antoine Pitrou | d741ed4 | 2016-12-27 14:23:43 +0100 | [diff] [blame] | 1503 | |
| 1504 | assert(_PyDict_CheckConsistency(mp)); |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1505 | return 0; |
| 1506 | } |
| 1507 | |
Raymond Hettinger | 4b74fba | 2014-05-03 16:32:11 -0700 | [diff] [blame] | 1508 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1509 | PyDict_DelItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1510 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1511 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | assert(key); |
| 1513 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1514 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | hash = PyObject_Hash(key); |
| 1516 | if (hash == -1) |
| 1517 | return -1; |
| 1518 | } |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1519 | |
| 1520 | return _PyDict_DelItem_KnownHash(op, key, hash); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Serhiy Storchaka | b9d98d5 | 2015-10-02 12:47:11 +0300 | [diff] [blame] | 1523 | int |
| 1524 | _PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash) |
| 1525 | { |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1526 | Py_ssize_t ix; |
Serhiy Storchaka | b9d98d5 | 2015-10-02 12:47:11 +0300 | [diff] [blame] | 1527 | PyDictObject *mp; |
Antoine Pitrou | c06ae20 | 2016-12-27 14:34:54 +0100 | [diff] [blame] | 1528 | PyObject *old_value; |
Serhiy Storchaka | b9d98d5 | 2015-10-02 12:47:11 +0300 | [diff] [blame] | 1529 | |
| 1530 | if (!PyDict_Check(op)) { |
| 1531 | PyErr_BadInternalCall(); |
| 1532 | return -1; |
| 1533 | } |
| 1534 | assert(key); |
| 1535 | assert(hash != -1); |
| 1536 | mp = (PyDictObject *)op; |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1537 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1538 | if (ix == DKIX_ERROR) |
Serhiy Storchaka | b9d98d5 | 2015-10-02 12:47:11 +0300 | [diff] [blame] | 1539 | return -1; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1540 | if (ix == DKIX_EMPTY || old_value == NULL) { |
Serhiy Storchaka | b9d98d5 | 2015-10-02 12:47:11 +0300 | [diff] [blame] | 1541 | _PyErr_SetKeyError(key); |
| 1542 | return -1; |
| 1543 | } |
Victor Stinner | 78601a3 | 2016-09-09 19:28:36 -0700 | [diff] [blame] | 1544 | |
| 1545 | // Split table doesn't allow deletion. Combine it. |
| 1546 | if (_PyDict_HasSplitTable(mp)) { |
| 1547 | if (dictresize(mp, DK_SIZE(mp->ma_keys))) { |
| 1548 | return -1; |
| 1549 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1550 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value); |
Victor Stinner | 78601a3 | 2016-09-09 19:28:36 -0700 | [diff] [blame] | 1551 | assert(ix >= 0); |
| 1552 | } |
| 1553 | |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1554 | return delitem_common(mp, hash, ix, old_value); |
Serhiy Storchaka | b9d98d5 | 2015-10-02 12:47:11 +0300 | [diff] [blame] | 1555 | } |
| 1556 | |
Antoine Pitrou | d741ed4 | 2016-12-27 14:23:43 +0100 | [diff] [blame] | 1557 | /* This function promises that the predicate -> deletion sequence is atomic |
| 1558 | * (i.e. protected by the GIL), assuming the predicate itself doesn't |
| 1559 | * release the GIL. |
| 1560 | */ |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1561 | int |
| 1562 | _PyDict_DelItemIf(PyObject *op, PyObject *key, |
| 1563 | int (*predicate)(PyObject *value)) |
| 1564 | { |
Antoine Pitrou | d741ed4 | 2016-12-27 14:23:43 +0100 | [diff] [blame] | 1565 | Py_ssize_t hashpos, ix; |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1566 | PyDictObject *mp; |
| 1567 | Py_hash_t hash; |
Antoine Pitrou | c06ae20 | 2016-12-27 14:34:54 +0100 | [diff] [blame] | 1568 | PyObject *old_value; |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1569 | int res; |
| 1570 | |
| 1571 | if (!PyDict_Check(op)) { |
| 1572 | PyErr_BadInternalCall(); |
| 1573 | return -1; |
| 1574 | } |
| 1575 | assert(key); |
| 1576 | hash = PyObject_Hash(key); |
| 1577 | if (hash == -1) |
| 1578 | return -1; |
| 1579 | mp = (PyDictObject *)op; |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1580 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value); |
Antoine Pitrou | d741ed4 | 2016-12-27 14:23:43 +0100 | [diff] [blame] | 1581 | if (ix == DKIX_ERROR) |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1582 | return -1; |
Antoine Pitrou | c06ae20 | 2016-12-27 14:34:54 +0100 | [diff] [blame] | 1583 | if (ix == DKIX_EMPTY || old_value == NULL) { |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1584 | _PyErr_SetKeyError(key); |
| 1585 | return -1; |
| 1586 | } |
Antoine Pitrou | d741ed4 | 2016-12-27 14:23:43 +0100 | [diff] [blame] | 1587 | |
| 1588 | // Split table doesn't allow deletion. Combine it. |
| 1589 | if (_PyDict_HasSplitTable(mp)) { |
| 1590 | if (dictresize(mp, DK_SIZE(mp->ma_keys))) { |
| 1591 | return -1; |
| 1592 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1593 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value); |
Antoine Pitrou | d741ed4 | 2016-12-27 14:23:43 +0100 | [diff] [blame] | 1594 | assert(ix >= 0); |
| 1595 | } |
| 1596 | |
Antoine Pitrou | c06ae20 | 2016-12-27 14:34:54 +0100 | [diff] [blame] | 1597 | res = predicate(old_value); |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1598 | if (res == -1) |
| 1599 | return -1; |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1600 | |
| 1601 | hashpos = lookdict_index(mp->ma_keys, hash, ix); |
| 1602 | assert(hashpos >= 0); |
| 1603 | |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1604 | if (res > 0) |
Antoine Pitrou | c06ae20 | 2016-12-27 14:34:54 +0100 | [diff] [blame] | 1605 | return delitem_common(mp, hashpos, ix, old_value); |
Antoine Pitrou | e10ca3a | 2016-12-27 14:19:20 +0100 | [diff] [blame] | 1606 | else |
| 1607 | return 0; |
| 1608 | } |
| 1609 | |
| 1610 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1611 | void |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1612 | PyDict_Clear(PyObject *op) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1613 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | PyDictObject *mp; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1615 | PyDictKeysObject *oldkeys; |
| 1616 | PyObject **oldvalues; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | Py_ssize_t i, n; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 1618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | if (!PyDict_Check(op)) |
| 1620 | return; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1621 | mp = ((PyDictObject *)op); |
| 1622 | oldkeys = mp->ma_keys; |
| 1623 | oldvalues = mp->ma_values; |
| 1624 | if (oldvalues == empty_values) |
| 1625 | return; |
| 1626 | /* Empty the dict... */ |
| 1627 | DK_INCREF(Py_EMPTY_KEYS); |
| 1628 | mp->ma_keys = Py_EMPTY_KEYS; |
| 1629 | mp->ma_values = empty_values; |
| 1630 | mp->ma_used = 0; |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 1631 | mp->ma_version_tag = DICT_NEXT_VERSION(); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1632 | /* ...then clear the keys and values */ |
| 1633 | if (oldvalues != NULL) { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1634 | n = oldkeys->dk_nentries; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1635 | for (i = 0; i < n; i++) |
| 1636 | Py_CLEAR(oldvalues[i]); |
| 1637 | free_values(oldvalues); |
| 1638 | DK_DECREF(oldkeys); |
| 1639 | } |
| 1640 | else { |
| 1641 | assert(oldkeys->dk_refcnt == 1); |
Antoine Pitrou | 2d169b2 | 2012-05-12 23:43:44 +0200 | [diff] [blame] | 1642 | DK_DECREF(oldkeys); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1643 | } |
Victor Stinner | 611b0fa | 2016-09-14 15:02:01 +0200 | [diff] [blame] | 1644 | assert(_PyDict_CheckConsistency(mp)); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1645 | } |
| 1646 | |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1647 | /* Internal version of PyDict_Next that returns a hash value in addition |
| 1648 | * to the key and value. |
| 1649 | * Return 1 on success, return 0 when the reached the end of the dictionary |
| 1650 | * (or if op is not a dictionary) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1651 | */ |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1652 | int |
| 1653 | _PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, |
| 1654 | PyObject **pvalue, Py_hash_t *phash) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1655 | { |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 1656 | Py_ssize_t i; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1657 | PyDictObject *mp; |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1658 | PyDictKeyEntry *entry_ptr; |
| 1659 | PyObject *value; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1660 | |
| 1661 | if (!PyDict_Check(op)) |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1662 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | mp = (PyDictObject *)op; |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1664 | i = *ppos; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1665 | if (mp->ma_values) { |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 1666 | if (i < 0 || i >= mp->ma_used) |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1667 | return 0; |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 1668 | /* values of split table is always dense */ |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1669 | entry_ptr = &DK_ENTRIES(mp->ma_keys)[i]; |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 1670 | value = mp->ma_values[i]; |
| 1671 | assert(value != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1672 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1673 | else { |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 1674 | Py_ssize_t n = mp->ma_keys->dk_nentries; |
| 1675 | if (i < 0 || i >= n) |
| 1676 | return 0; |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1677 | entry_ptr = &DK_ENTRIES(mp->ma_keys)[i]; |
| 1678 | while (i < n && entry_ptr->me_value == NULL) { |
| 1679 | entry_ptr++; |
| 1680 | i++; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1681 | } |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1682 | if (i >= n) |
| 1683 | return 0; |
| 1684 | value = entry_ptr->me_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | } |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1686 | *ppos = i+1; |
| 1687 | if (pkey) |
| 1688 | *pkey = entry_ptr->me_key; |
| 1689 | if (phash) |
| 1690 | *phash = entry_ptr->me_hash; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1691 | if (pvalue) |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1692 | *pvalue = value; |
| 1693 | return 1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1696 | /* |
| 1697 | * Iterate over a dict. Use like so: |
| 1698 | * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1699 | * Py_ssize_t i; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1700 | * PyObject *key, *value; |
| 1701 | * i = 0; # important! i should not otherwise be changed by you |
Neal Norwitz | 0732301 | 2003-02-15 14:45:12 +0000 | [diff] [blame] | 1702 | * while (PyDict_Next(yourdict, &i, &key, &value)) { |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1703 | * Refer to borrowed references in key and value. |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1704 | * } |
| 1705 | * |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1706 | * Return 1 on success, return 0 when the reached the end of the dictionary |
| 1707 | * (or if op is not a dictionary) |
| 1708 | * |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1709 | * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that |
Tim Peters | 6783070 | 2001-03-21 19:23:56 +0000 | [diff] [blame] | 1710 | * mutates the dict. One exception: it is safe if the loop merely changes |
| 1711 | * the values associated with the keys (but doesn't insert new keys or |
| 1712 | * delete keys), via PyDict_SetItem(). |
| 1713 | */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1714 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1715 | PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1716 | { |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 1717 | return _PyDict_Next(op, ppos, pkey, pvalue, NULL); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1720 | /* Internal version of dict.pop(). */ |
| 1721 | PyObject * |
Serhiy Storchaka | 42e1ea9 | 2017-01-12 19:12:21 +0200 | [diff] [blame] | 1722 | _PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1723 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1724 | Py_ssize_t ix, hashpos; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1725 | PyObject *old_value, *old_key; |
| 1726 | PyDictKeyEntry *ep; |
Yury Selivanov | 684ef2c | 2016-10-28 19:01:21 -0400 | [diff] [blame] | 1727 | PyDictObject *mp; |
| 1728 | |
| 1729 | assert(PyDict_Check(dict)); |
| 1730 | mp = (PyDictObject *)dict; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1731 | |
| 1732 | if (mp->ma_used == 0) { |
| 1733 | if (deflt) { |
| 1734 | Py_INCREF(deflt); |
| 1735 | return deflt; |
| 1736 | } |
| 1737 | _PyErr_SetKeyError(key); |
| 1738 | return NULL; |
| 1739 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1740 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1741 | if (ix == DKIX_ERROR) |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1742 | return NULL; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1743 | if (ix == DKIX_EMPTY || old_value == NULL) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1744 | if (deflt) { |
| 1745 | Py_INCREF(deflt); |
| 1746 | return deflt; |
| 1747 | } |
| 1748 | _PyErr_SetKeyError(key); |
| 1749 | return NULL; |
| 1750 | } |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 1751 | |
Victor Stinner | 78601a3 | 2016-09-09 19:28:36 -0700 | [diff] [blame] | 1752 | // Split table doesn't allow deletion. Combine it. |
| 1753 | if (_PyDict_HasSplitTable(mp)) { |
| 1754 | if (dictresize(mp, DK_SIZE(mp->ma_keys))) { |
| 1755 | return NULL; |
| 1756 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1757 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value); |
Victor Stinner | 78601a3 | 2016-09-09 19:28:36 -0700 | [diff] [blame] | 1758 | assert(ix >= 0); |
| 1759 | } |
| 1760 | |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 1761 | hashpos = lookdict_index(mp->ma_keys, hash, ix); |
| 1762 | assert(hashpos >= 0); |
Victor Stinner | 78601a3 | 2016-09-09 19:28:36 -0700 | [diff] [blame] | 1763 | assert(old_value != NULL); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1764 | mp->ma_used--; |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 1765 | mp->ma_version_tag = DICT_NEXT_VERSION(); |
Victor Stinner | 78601a3 | 2016-09-09 19:28:36 -0700 | [diff] [blame] | 1766 | dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY); |
| 1767 | ep = &DK_ENTRIES(mp->ma_keys)[ix]; |
| 1768 | ENSURE_ALLOWS_DELETIONS(mp); |
| 1769 | old_key = ep->me_key; |
| 1770 | ep->me_key = NULL; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 1771 | ep->me_value = NULL; |
Victor Stinner | 78601a3 | 2016-09-09 19:28:36 -0700 | [diff] [blame] | 1772 | Py_DECREF(old_key); |
Victor Stinner | 611b0fa | 2016-09-14 15:02:01 +0200 | [diff] [blame] | 1773 | |
| 1774 | assert(_PyDict_CheckConsistency(mp)); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1775 | return old_value; |
| 1776 | } |
| 1777 | |
Serhiy Storchaka | 6779652 | 2017-01-12 18:34:33 +0200 | [diff] [blame] | 1778 | PyObject * |
Serhiy Storchaka | 42e1ea9 | 2017-01-12 19:12:21 +0200 | [diff] [blame] | 1779 | _PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt) |
Serhiy Storchaka | 6779652 | 2017-01-12 18:34:33 +0200 | [diff] [blame] | 1780 | { |
| 1781 | Py_hash_t hash; |
| 1782 | |
Serhiy Storchaka | 42e1ea9 | 2017-01-12 19:12:21 +0200 | [diff] [blame] | 1783 | if (((PyDictObject *)dict)->ma_used == 0) { |
Serhiy Storchaka | 6779652 | 2017-01-12 18:34:33 +0200 | [diff] [blame] | 1784 | if (deflt) { |
| 1785 | Py_INCREF(deflt); |
| 1786 | return deflt; |
| 1787 | } |
| 1788 | _PyErr_SetKeyError(key); |
| 1789 | return NULL; |
| 1790 | } |
| 1791 | if (!PyUnicode_CheckExact(key) || |
| 1792 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
| 1793 | hash = PyObject_Hash(key); |
| 1794 | if (hash == -1) |
| 1795 | return NULL; |
| 1796 | } |
Serhiy Storchaka | 42e1ea9 | 2017-01-12 19:12:21 +0200 | [diff] [blame] | 1797 | return _PyDict_Pop_KnownHash(dict, key, hash, deflt); |
Serhiy Storchaka | 6779652 | 2017-01-12 18:34:33 +0200 | [diff] [blame] | 1798 | } |
| 1799 | |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1800 | /* Internal version of dict.from_keys(). It is subclass-friendly. */ |
| 1801 | PyObject * |
| 1802 | _PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value) |
| 1803 | { |
| 1804 | PyObject *it; /* iter(iterable) */ |
| 1805 | PyObject *key; |
| 1806 | PyObject *d; |
| 1807 | int status; |
| 1808 | |
Victor Stinner | a5ed5f0 | 2016-12-06 18:45:50 +0100 | [diff] [blame] | 1809 | d = _PyObject_CallNoArg(cls); |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1810 | if (d == NULL) |
| 1811 | return NULL; |
| 1812 | |
| 1813 | if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) { |
| 1814 | if (PyDict_CheckExact(iterable)) { |
| 1815 | PyDictObject *mp = (PyDictObject *)d; |
| 1816 | PyObject *oldvalue; |
| 1817 | Py_ssize_t pos = 0; |
| 1818 | PyObject *key; |
| 1819 | Py_hash_t hash; |
| 1820 | |
Serhiy Storchaka | c61ac16 | 2017-03-21 08:52:38 +0200 | [diff] [blame] | 1821 | if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1822 | Py_DECREF(d); |
| 1823 | return NULL; |
| 1824 | } |
| 1825 | |
| 1826 | while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) { |
| 1827 | if (insertdict(mp, key, hash, value)) { |
| 1828 | Py_DECREF(d); |
| 1829 | return NULL; |
| 1830 | } |
| 1831 | } |
| 1832 | return d; |
| 1833 | } |
| 1834 | if (PyAnySet_CheckExact(iterable)) { |
| 1835 | PyDictObject *mp = (PyDictObject *)d; |
| 1836 | Py_ssize_t pos = 0; |
| 1837 | PyObject *key; |
| 1838 | Py_hash_t hash; |
| 1839 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1840 | if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 1841 | Py_DECREF(d); |
| 1842 | return NULL; |
| 1843 | } |
| 1844 | |
| 1845 | while (_PySet_NextEntry(iterable, &pos, &key, &hash)) { |
| 1846 | if (insertdict(mp, key, hash, value)) { |
| 1847 | Py_DECREF(d); |
| 1848 | return NULL; |
| 1849 | } |
| 1850 | } |
| 1851 | return d; |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | it = PyObject_GetIter(iterable); |
| 1856 | if (it == NULL){ |
| 1857 | Py_DECREF(d); |
| 1858 | return NULL; |
| 1859 | } |
| 1860 | |
| 1861 | if (PyDict_CheckExact(d)) { |
| 1862 | while ((key = PyIter_Next(it)) != NULL) { |
| 1863 | status = PyDict_SetItem(d, key, value); |
| 1864 | Py_DECREF(key); |
| 1865 | if (status < 0) |
| 1866 | goto Fail; |
| 1867 | } |
| 1868 | } else { |
| 1869 | while ((key = PyIter_Next(it)) != NULL) { |
| 1870 | status = PyObject_SetItem(d, key, value); |
| 1871 | Py_DECREF(key); |
| 1872 | if (status < 0) |
| 1873 | goto Fail; |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | if (PyErr_Occurred()) |
| 1878 | goto Fail; |
| 1879 | Py_DECREF(it); |
| 1880 | return d; |
| 1881 | |
| 1882 | Fail: |
| 1883 | Py_DECREF(it); |
| 1884 | Py_DECREF(d); |
| 1885 | return NULL; |
| 1886 | } |
| 1887 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1888 | /* Methods */ |
| 1889 | |
| 1890 | static void |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1891 | dict_dealloc(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1892 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1893 | PyObject **values = mp->ma_values; |
| 1894 | PyDictKeysObject *keys = mp->ma_keys; |
| 1895 | Py_ssize_t i, n; |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 1896 | |
| 1897 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | PyObject_GC_UnTrack(mp); |
| 1899 | Py_TRASHCAN_SAFE_BEGIN(mp) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1900 | if (values != NULL) { |
| 1901 | if (values != empty_values) { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 1902 | for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1903 | Py_XDECREF(values[i]); |
| 1904 | } |
| 1905 | free_values(values); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1907 | DK_DECREF(keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1908 | } |
Victor Stinner | ac2a4fe | 2013-07-16 22:19:00 +0200 | [diff] [blame] | 1909 | else if (keys != NULL) { |
Antoine Pitrou | 2d169b2 | 2012-05-12 23:43:44 +0200 | [diff] [blame] | 1910 | assert(keys->dk_refcnt == 1); |
| 1911 | DK_DECREF(keys); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1912 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1913 | if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type) |
| 1914 | free_list[numfree++] = mp; |
| 1915 | else |
| 1916 | Py_TYPE(mp)->tp_free((PyObject *)mp); |
| 1917 | Py_TRASHCAN_SAFE_END(mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1920 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1921 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1922 | dict_repr(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1923 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1924 | Py_ssize_t i; |
Victor Stinner | f91929b | 2013-11-19 13:07:38 +0100 | [diff] [blame] | 1925 | PyObject *key = NULL, *value = NULL; |
| 1926 | _PyUnicodeWriter writer; |
| 1927 | int first; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 1928 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1929 | i = Py_ReprEnter((PyObject *)mp); |
| 1930 | if (i != 0) { |
| 1931 | return i > 0 ? PyUnicode_FromString("{...}") : NULL; |
| 1932 | } |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 1933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | if (mp->ma_used == 0) { |
Victor Stinner | f91929b | 2013-11-19 13:07:38 +0100 | [diff] [blame] | 1935 | Py_ReprLeave((PyObject *)mp); |
| 1936 | return PyUnicode_FromString("{}"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1937 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1938 | |
Victor Stinner | f91929b | 2013-11-19 13:07:38 +0100 | [diff] [blame] | 1939 | _PyUnicodeWriter_Init(&writer); |
| 1940 | writer.overallocate = 1; |
| 1941 | /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */ |
| 1942 | writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1943 | |
Victor Stinner | f91929b | 2013-11-19 13:07:38 +0100 | [diff] [blame] | 1944 | if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0) |
| 1945 | goto error; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1947 | /* Do repr() on each key+value pair, and insert ": " between them. |
| 1948 | Note that repr may mutate the dict. */ |
| 1949 | i = 0; |
Victor Stinner | f91929b | 2013-11-19 13:07:38 +0100 | [diff] [blame] | 1950 | first = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1951 | while (PyDict_Next((PyObject *)mp, &i, &key, &value)) { |
Victor Stinner | f91929b | 2013-11-19 13:07:38 +0100 | [diff] [blame] | 1952 | PyObject *s; |
| 1953 | int res; |
| 1954 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1955 | /* Prevent repr from deleting key or value during key format. */ |
| 1956 | Py_INCREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1957 | Py_INCREF(value); |
Victor Stinner | f97dfd7 | 2013-07-18 01:00:45 +0200 | [diff] [blame] | 1958 | |
Victor Stinner | f91929b | 2013-11-19 13:07:38 +0100 | [diff] [blame] | 1959 | if (!first) { |
| 1960 | if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) |
| 1961 | goto error; |
| 1962 | } |
| 1963 | first = 0; |
| 1964 | |
| 1965 | s = PyObject_Repr(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1966 | if (s == NULL) |
Victor Stinner | f91929b | 2013-11-19 13:07:38 +0100 | [diff] [blame] | 1967 | goto error; |
| 1968 | res = _PyUnicodeWriter_WriteStr(&writer, s); |
| 1969 | Py_DECREF(s); |
| 1970 | if (res < 0) |
| 1971 | goto error; |
| 1972 | |
| 1973 | if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0) |
| 1974 | goto error; |
| 1975 | |
| 1976 | s = PyObject_Repr(value); |
| 1977 | if (s == NULL) |
| 1978 | goto error; |
| 1979 | res = _PyUnicodeWriter_WriteStr(&writer, s); |
| 1980 | Py_DECREF(s); |
| 1981 | if (res < 0) |
| 1982 | goto error; |
| 1983 | |
| 1984 | Py_CLEAR(key); |
| 1985 | Py_CLEAR(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1987 | |
Victor Stinner | f91929b | 2013-11-19 13:07:38 +0100 | [diff] [blame] | 1988 | writer.overallocate = 0; |
| 1989 | if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0) |
| 1990 | goto error; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | Py_ReprLeave((PyObject *)mp); |
Victor Stinner | f91929b | 2013-11-19 13:07:38 +0100 | [diff] [blame] | 1993 | |
| 1994 | return _PyUnicodeWriter_Finish(&writer); |
| 1995 | |
| 1996 | error: |
| 1997 | Py_ReprLeave((PyObject *)mp); |
| 1998 | _PyUnicodeWriter_Dealloc(&writer); |
| 1999 | Py_XDECREF(key); |
| 2000 | Py_XDECREF(value); |
| 2001 | return NULL; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2004 | static Py_ssize_t |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2005 | dict_length(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2006 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2007 | return mp->ma_used; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2010 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2011 | dict_subscript(PyDictObject *mp, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2012 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2013 | Py_ssize_t ix; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2014 | Py_hash_t hash; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 2015 | PyObject *value; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2016 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2017 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2018 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2019 | hash = PyObject_Hash(key); |
| 2020 | if (hash == -1) |
| 2021 | return NULL; |
| 2022 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 2023 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2024 | if (ix == DKIX_ERROR) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2025 | return NULL; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 2026 | if (ix == DKIX_EMPTY || value == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | if (!PyDict_CheckExact(mp)) { |
| 2028 | /* Look up __missing__ method if we're a subclass. */ |
| 2029 | PyObject *missing, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 2030 | _Py_IDENTIFIER(__missing__); |
| 2031 | missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2032 | if (missing != NULL) { |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 2033 | res = PyObject_CallFunctionObjArgs(missing, |
| 2034 | key, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | Py_DECREF(missing); |
| 2036 | return res; |
| 2037 | } |
| 2038 | else if (PyErr_Occurred()) |
| 2039 | return NULL; |
| 2040 | } |
Raymond Hettinger | 69492da | 2013-09-02 15:59:26 -0700 | [diff] [blame] | 2041 | _PyErr_SetKeyError(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2042 | return NULL; |
| 2043 | } |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 2044 | Py_INCREF(value); |
| 2045 | return value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | static int |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2049 | dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2050 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2051 | if (w == NULL) |
| 2052 | return PyDict_DelItem((PyObject *)mp, v); |
| 2053 | else |
| 2054 | return PyDict_SetItem((PyObject *)mp, v, w); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 2057 | static PyMappingMethods dict_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2058 | (lenfunc)dict_length, /*mp_length*/ |
| 2059 | (binaryfunc)dict_subscript, /*mp_subscript*/ |
| 2060 | (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2061 | }; |
| 2062 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2063 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2064 | dict_keys(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2065 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2066 | PyObject *v; |
| 2067 | Py_ssize_t i, j; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2068 | PyDictKeyEntry *ep; |
| 2069 | Py_ssize_t size, n, offset; |
| 2070 | PyObject **value_ptr; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 2071 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 2072 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2073 | n = mp->ma_used; |
| 2074 | v = PyList_New(n); |
| 2075 | if (v == NULL) |
| 2076 | return NULL; |
| 2077 | if (n != mp->ma_used) { |
| 2078 | /* Durnit. The allocations caused the dict to resize. |
| 2079 | * Just start over, this shouldn't normally happen. |
| 2080 | */ |
| 2081 | Py_DECREF(v); |
| 2082 | goto again; |
| 2083 | } |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2084 | ep = DK_ENTRIES(mp->ma_keys); |
| 2085 | size = mp->ma_keys->dk_nentries; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2086 | if (mp->ma_values) { |
| 2087 | value_ptr = mp->ma_values; |
| 2088 | offset = sizeof(PyObject *); |
| 2089 | } |
| 2090 | else { |
| 2091 | value_ptr = &ep[0].me_value; |
| 2092 | offset = sizeof(PyDictKeyEntry); |
| 2093 | } |
| 2094 | for (i = 0, j = 0; i < size; i++) { |
| 2095 | if (*value_ptr != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2096 | PyObject *key = ep[i].me_key; |
| 2097 | Py_INCREF(key); |
| 2098 | PyList_SET_ITEM(v, j, key); |
| 2099 | j++; |
| 2100 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2101 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | } |
| 2103 | assert(j == n); |
| 2104 | return v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2107 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2108 | dict_values(PyDictObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2109 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2110 | PyObject *v; |
| 2111 | Py_ssize_t i, j; |
Benjamin Peterson | f0acae2 | 2016-09-08 09:50:08 -0700 | [diff] [blame] | 2112 | PyDictKeyEntry *ep; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2113 | Py_ssize_t size, n, offset; |
| 2114 | PyObject **value_ptr; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 2115 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 2116 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2117 | n = mp->ma_used; |
| 2118 | v = PyList_New(n); |
| 2119 | if (v == NULL) |
| 2120 | return NULL; |
| 2121 | if (n != mp->ma_used) { |
| 2122 | /* Durnit. The allocations caused the dict to resize. |
| 2123 | * Just start over, this shouldn't normally happen. |
| 2124 | */ |
| 2125 | Py_DECREF(v); |
| 2126 | goto again; |
| 2127 | } |
Benjamin Peterson | f0acae2 | 2016-09-08 09:50:08 -0700 | [diff] [blame] | 2128 | ep = DK_ENTRIES(mp->ma_keys); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2129 | size = mp->ma_keys->dk_nentries; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2130 | if (mp->ma_values) { |
| 2131 | value_ptr = mp->ma_values; |
| 2132 | offset = sizeof(PyObject *); |
| 2133 | } |
| 2134 | else { |
Benjamin Peterson | f0acae2 | 2016-09-08 09:50:08 -0700 | [diff] [blame] | 2135 | value_ptr = &ep[0].me_value; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2136 | offset = sizeof(PyDictKeyEntry); |
| 2137 | } |
| 2138 | for (i = 0, j = 0; i < size; i++) { |
| 2139 | PyObject *value = *value_ptr; |
| 2140 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
| 2141 | if (value != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2142 | Py_INCREF(value); |
| 2143 | PyList_SET_ITEM(v, j, value); |
| 2144 | j++; |
| 2145 | } |
| 2146 | } |
| 2147 | assert(j == n); |
| 2148 | return v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2151 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2152 | dict_items(PyDictObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2153 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2154 | PyObject *v; |
| 2155 | Py_ssize_t i, j, n; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2156 | Py_ssize_t size, offset; |
| 2157 | PyObject *item, *key; |
| 2158 | PyDictKeyEntry *ep; |
| 2159 | PyObject **value_ptr; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 2160 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | /* Preallocate the list of tuples, to avoid allocations during |
| 2162 | * the loop over the items, which could trigger GC, which |
| 2163 | * could resize the dict. :-( |
| 2164 | */ |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 2165 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | n = mp->ma_used; |
| 2167 | v = PyList_New(n); |
| 2168 | if (v == NULL) |
| 2169 | return NULL; |
| 2170 | for (i = 0; i < n; i++) { |
| 2171 | item = PyTuple_New(2); |
| 2172 | if (item == NULL) { |
| 2173 | Py_DECREF(v); |
| 2174 | return NULL; |
| 2175 | } |
| 2176 | PyList_SET_ITEM(v, i, item); |
| 2177 | } |
| 2178 | if (n != mp->ma_used) { |
| 2179 | /* Durnit. The allocations caused the dict to resize. |
| 2180 | * Just start over, this shouldn't normally happen. |
| 2181 | */ |
| 2182 | Py_DECREF(v); |
| 2183 | goto again; |
| 2184 | } |
| 2185 | /* Nothing we do below makes any function calls. */ |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2186 | ep = DK_ENTRIES(mp->ma_keys); |
| 2187 | size = mp->ma_keys->dk_nentries; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2188 | if (mp->ma_values) { |
| 2189 | value_ptr = mp->ma_values; |
| 2190 | offset = sizeof(PyObject *); |
| 2191 | } |
| 2192 | else { |
| 2193 | value_ptr = &ep[0].me_value; |
| 2194 | offset = sizeof(PyDictKeyEntry); |
| 2195 | } |
| 2196 | for (i = 0, j = 0; i < size; i++) { |
| 2197 | PyObject *value = *value_ptr; |
| 2198 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
| 2199 | if (value != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2200 | key = ep[i].me_key; |
| 2201 | item = PyList_GET_ITEM(v, j); |
| 2202 | Py_INCREF(key); |
| 2203 | PyTuple_SET_ITEM(item, 0, key); |
| 2204 | Py_INCREF(value); |
| 2205 | PyTuple_SET_ITEM(item, 1, value); |
| 2206 | j++; |
| 2207 | } |
| 2208 | } |
| 2209 | assert(j == n); |
| 2210 | return v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 2213 | /*[clinic input] |
| 2214 | @classmethod |
| 2215 | dict.fromkeys |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 2216 | iterable: object |
| 2217 | value: object=None |
| 2218 | / |
| 2219 | |
Serhiy Storchaka | 78d9e58 | 2017-01-25 00:30:04 +0200 | [diff] [blame] | 2220 | Create a new dictionary with keys from iterable and values set to value. |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 2221 | [clinic start generated code]*/ |
| 2222 | |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 2223 | static PyObject * |
| 2224 | dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value) |
Serhiy Storchaka | 78d9e58 | 2017-01-25 00:30:04 +0200 | [diff] [blame] | 2225 | /*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/ |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 2226 | { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 2227 | return _PyDict_FromKeys((PyObject *)type, iterable, value); |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 2230 | static int |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2231 | dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, |
| 2232 | const char *methname) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 2233 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2234 | PyObject *arg = NULL; |
| 2235 | int result = 0; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 2236 | |
Serhiy Storchaka | 60c3d35 | 2017-11-11 16:19:56 +0200 | [diff] [blame] | 2237 | if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2238 | result = -1; |
Serhiy Storchaka | 60c3d35 | 2017-11-11 16:19:56 +0200 | [diff] [blame] | 2239 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2240 | else if (arg != NULL) { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 2241 | _Py_IDENTIFIER(keys); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 2242 | PyObject *func; |
| 2243 | if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) { |
| 2244 | result = -1; |
| 2245 | } |
| 2246 | else if (func != NULL) { |
Serhiy Storchaka | 60c3d35 | 2017-11-11 16:19:56 +0200 | [diff] [blame] | 2247 | Py_DECREF(func); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2248 | result = PyDict_Merge(self, arg, 1); |
Serhiy Storchaka | 60c3d35 | 2017-11-11 16:19:56 +0200 | [diff] [blame] | 2249 | } |
Serhiy Storchaka | 60c3d35 | 2017-11-11 16:19:56 +0200 | [diff] [blame] | 2250 | else { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 2251 | result = PyDict_MergeFromSeq2(self, arg, 1); |
Serhiy Storchaka | 60c3d35 | 2017-11-11 16:19:56 +0200 | [diff] [blame] | 2252 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 | } |
Serhiy Storchaka | 60c3d35 | 2017-11-11 16:19:56 +0200 | [diff] [blame] | 2254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 | if (result == 0 && kwds != NULL) { |
| 2256 | if (PyArg_ValidateKeywordArguments(kwds)) |
| 2257 | result = PyDict_Merge(self, kwds, 1); |
| 2258 | else |
| 2259 | result = -1; |
| 2260 | } |
| 2261 | return result; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Victor Stinner | 91f0d4a | 2017-01-19 12:45:06 +0100 | [diff] [blame] | 2264 | /* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention. |
Serhiy Storchaka | 6969eaf | 2017-07-03 21:20:15 +0300 | [diff] [blame] | 2265 | Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls |
| 2266 | slower, see the issue #29312. */ |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 2267 | static PyObject * |
| 2268 | dict_update(PyObject *self, PyObject *args, PyObject *kwds) |
| 2269 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | if (dict_update_common(self, args, kwds, "update") != -1) |
| 2271 | Py_RETURN_NONE; |
| 2272 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 2275 | /* Update unconditionally replaces existing items. |
| 2276 | Merge has a 3rd argument 'override'; if set, it acts like Update, |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2277 | otherwise it leaves existing items unchanged. |
| 2278 | |
| 2279 | PyDict_{Update,Merge} update/merge from a mapping object. |
| 2280 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 2281 | PyDict_MergeFromSeq2 updates/merges from any iterable object |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2282 | producing iterable objects of length 2. |
| 2283 | */ |
| 2284 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 2285 | int |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2286 | PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) |
| 2287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | PyObject *it; /* iter(seq2) */ |
| 2289 | Py_ssize_t i; /* index into seq2 of current element */ |
| 2290 | PyObject *item; /* seq2[i] */ |
| 2291 | PyObject *fast; /* item as a 2-tuple or 2-list */ |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2293 | assert(d != NULL); |
| 2294 | assert(PyDict_Check(d)); |
| 2295 | assert(seq2 != NULL); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2296 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2297 | it = PyObject_GetIter(seq2); |
| 2298 | if (it == NULL) |
| 2299 | return -1; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | for (i = 0; ; ++i) { |
| 2302 | PyObject *key, *value; |
| 2303 | Py_ssize_t n; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2305 | fast = NULL; |
| 2306 | item = PyIter_Next(it); |
| 2307 | if (item == NULL) { |
| 2308 | if (PyErr_Occurred()) |
| 2309 | goto Fail; |
| 2310 | break; |
| 2311 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | /* Convert item to sequence, and verify length 2. */ |
| 2314 | fast = PySequence_Fast(item, ""); |
| 2315 | if (fast == NULL) { |
| 2316 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 2317 | PyErr_Format(PyExc_TypeError, |
| 2318 | "cannot convert dictionary update " |
| 2319 | "sequence element #%zd to a sequence", |
| 2320 | i); |
| 2321 | goto Fail; |
| 2322 | } |
| 2323 | n = PySequence_Fast_GET_SIZE(fast); |
| 2324 | if (n != 2) { |
| 2325 | PyErr_Format(PyExc_ValueError, |
| 2326 | "dictionary update sequence element #%zd " |
| 2327 | "has length %zd; 2 is required", |
| 2328 | i, n); |
| 2329 | goto Fail; |
| 2330 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | /* Update/merge with this (key, value) pair. */ |
| 2333 | key = PySequence_Fast_GET_ITEM(fast, 0); |
| 2334 | value = PySequence_Fast_GET_ITEM(fast, 1); |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 2335 | Py_INCREF(key); |
| 2336 | Py_INCREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2337 | if (override || PyDict_GetItem(d, key) == NULL) { |
| 2338 | int status = PyDict_SetItem(d, key, value); |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 2339 | if (status < 0) { |
| 2340 | Py_DECREF(key); |
| 2341 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2342 | goto Fail; |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 2343 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 | } |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 2345 | Py_DECREF(key); |
| 2346 | Py_DECREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2347 | Py_DECREF(fast); |
| 2348 | Py_DECREF(item); |
| 2349 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2351 | i = 0; |
Victor Stinner | 611b0fa | 2016-09-14 15:02:01 +0200 | [diff] [blame] | 2352 | assert(_PyDict_CheckConsistency((PyDictObject *)d)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2353 | goto Return; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2354 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2355 | Py_XDECREF(item); |
| 2356 | Py_XDECREF(fast); |
| 2357 | i = -1; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2358 | Return: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2359 | Py_DECREF(it); |
| 2360 | return Py_SAFE_DOWNCAST(i, Py_ssize_t, int); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
doko@ubuntu.com | c96df68 | 2016-10-11 08:04:02 +0200 | [diff] [blame] | 2363 | static int |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2364 | dict_merge(PyObject *a, PyObject *b, int override) |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 2365 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2366 | PyDictObject *mp, *other; |
| 2367 | Py_ssize_t i, n; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2368 | PyDictKeyEntry *entry, *ep0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2369 | |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2370 | assert(0 <= override && override <= 2); |
| 2371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2372 | /* We accept for the argument either a concrete dictionary object, |
| 2373 | * or an abstract "mapping" object. For the former, we can do |
| 2374 | * things quite efficiently. For the latter, we only require that |
| 2375 | * PyMapping_Keys() and PyObject_GetItem() be supported. |
| 2376 | */ |
| 2377 | if (a == NULL || !PyDict_Check(a) || b == NULL) { |
| 2378 | PyErr_BadInternalCall(); |
| 2379 | return -1; |
| 2380 | } |
| 2381 | mp = (PyDictObject*)a; |
| 2382 | if (PyDict_Check(b)) { |
| 2383 | other = (PyDictObject*)b; |
| 2384 | if (other == mp || other->ma_used == 0) |
| 2385 | /* a.update(a) or a.update({}); nothing to do */ |
| 2386 | return 0; |
| 2387 | if (mp->ma_used == 0) |
| 2388 | /* Since the target dict is empty, PyDict_GetItem() |
| 2389 | * always returns NULL. Setting override to 1 |
| 2390 | * skips the unnecessary test. |
| 2391 | */ |
| 2392 | override = 1; |
| 2393 | /* Do one big resize at the start, rather than |
| 2394 | * incrementally resizing as we insert new items. Expect |
| 2395 | * that there will be no (or few) overlapping keys. |
| 2396 | */ |
INADA Naoki | b1152be | 2016-10-27 19:26:50 +0900 | [diff] [blame] | 2397 | if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) { |
| 2398 | if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2399 | return -1; |
INADA Naoki | b1152be | 2016-10-27 19:26:50 +0900 | [diff] [blame] | 2400 | } |
| 2401 | } |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2402 | ep0 = DK_ENTRIES(other->ma_keys); |
| 2403 | for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) { |
Benjamin Peterson | a82f77f | 2015-07-04 19:55:16 -0500 | [diff] [blame] | 2404 | PyObject *key, *value; |
| 2405 | Py_hash_t hash; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2406 | entry = &ep0[i]; |
Benjamin Peterson | a82f77f | 2015-07-04 19:55:16 -0500 | [diff] [blame] | 2407 | key = entry->me_key; |
| 2408 | hash = entry->me_hash; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2409 | if (other->ma_values) |
| 2410 | value = other->ma_values[i]; |
| 2411 | else |
| 2412 | value = entry->me_value; |
| 2413 | |
Benjamin Peterson | a82f77f | 2015-07-04 19:55:16 -0500 | [diff] [blame] | 2414 | if (value != NULL) { |
| 2415 | int err = 0; |
| 2416 | Py_INCREF(key); |
| 2417 | Py_INCREF(value); |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 2418 | if (override == 1) |
Benjamin Peterson | a82f77f | 2015-07-04 19:55:16 -0500 | [diff] [blame] | 2419 | err = insertdict(mp, key, hash, value); |
Serhiy Storchaka | f0b311b | 2016-11-06 13:18:24 +0200 | [diff] [blame] | 2420 | else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) { |
| 2421 | if (PyErr_Occurred()) { |
| 2422 | Py_DECREF(value); |
| 2423 | Py_DECREF(key); |
| 2424 | return -1; |
| 2425 | } |
| 2426 | err = insertdict(mp, key, hash, value); |
| 2427 | } |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2428 | else if (override != 0) { |
| 2429 | _PyErr_SetKeyError(key); |
| 2430 | Py_DECREF(value); |
| 2431 | Py_DECREF(key); |
| 2432 | return -1; |
| 2433 | } |
Benjamin Peterson | a82f77f | 2015-07-04 19:55:16 -0500 | [diff] [blame] | 2434 | Py_DECREF(value); |
| 2435 | Py_DECREF(key); |
| 2436 | if (err != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2437 | return -1; |
Benjamin Peterson | a82f77f | 2015-07-04 19:55:16 -0500 | [diff] [blame] | 2438 | |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2439 | if (n != other->ma_keys->dk_nentries) { |
Benjamin Peterson | a82f77f | 2015-07-04 19:55:16 -0500 | [diff] [blame] | 2440 | PyErr_SetString(PyExc_RuntimeError, |
| 2441 | "dict mutated during update"); |
| 2442 | return -1; |
| 2443 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | } |
| 2445 | } |
| 2446 | } |
| 2447 | else { |
| 2448 | /* Do it the generic, slower way */ |
| 2449 | PyObject *keys = PyMapping_Keys(b); |
| 2450 | PyObject *iter; |
| 2451 | PyObject *key, *value; |
| 2452 | int status; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 2453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2454 | if (keys == NULL) |
| 2455 | /* Docstring says this is equivalent to E.keys() so |
| 2456 | * if E doesn't have a .keys() method we want |
| 2457 | * AttributeError to percolate up. Might as well |
| 2458 | * do the same for any other error. |
| 2459 | */ |
| 2460 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 2461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2462 | iter = PyObject_GetIter(keys); |
| 2463 | Py_DECREF(keys); |
| 2464 | if (iter == NULL) |
| 2465 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 2466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2467 | for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) { |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2468 | if (override != 1 && PyDict_GetItem(a, key) != NULL) { |
| 2469 | if (override != 0) { |
| 2470 | _PyErr_SetKeyError(key); |
| 2471 | Py_DECREF(key); |
| 2472 | Py_DECREF(iter); |
| 2473 | return -1; |
| 2474 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2475 | Py_DECREF(key); |
| 2476 | continue; |
| 2477 | } |
| 2478 | value = PyObject_GetItem(b, key); |
| 2479 | if (value == NULL) { |
| 2480 | Py_DECREF(iter); |
| 2481 | Py_DECREF(key); |
| 2482 | return -1; |
| 2483 | } |
| 2484 | status = PyDict_SetItem(a, key, value); |
| 2485 | Py_DECREF(key); |
| 2486 | Py_DECREF(value); |
| 2487 | if (status < 0) { |
| 2488 | Py_DECREF(iter); |
| 2489 | return -1; |
| 2490 | } |
| 2491 | } |
| 2492 | Py_DECREF(iter); |
| 2493 | if (PyErr_Occurred()) |
| 2494 | /* Iterator completed, via error */ |
| 2495 | return -1; |
| 2496 | } |
Victor Stinner | 611b0fa | 2016-09-14 15:02:01 +0200 | [diff] [blame] | 2497 | assert(_PyDict_CheckConsistency((PyDictObject *)a)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2498 | return 0; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
Serhiy Storchaka | e036ef8 | 2016-10-02 11:06:43 +0300 | [diff] [blame] | 2501 | int |
| 2502 | PyDict_Update(PyObject *a, PyObject *b) |
| 2503 | { |
| 2504 | return dict_merge(a, b, 1); |
| 2505 | } |
| 2506 | |
| 2507 | int |
| 2508 | PyDict_Merge(PyObject *a, PyObject *b, int override) |
| 2509 | { |
| 2510 | /* XXX Deprecate override not in (0, 1). */ |
| 2511 | return dict_merge(a, b, override != 0); |
| 2512 | } |
| 2513 | |
| 2514 | int |
| 2515 | _PyDict_MergeEx(PyObject *a, PyObject *b, int override) |
| 2516 | { |
| 2517 | return dict_merge(a, b, override); |
| 2518 | } |
| 2519 | |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 2520 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2521 | dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 2522 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | return PyDict_Copy((PyObject*)mp); |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2527 | PyDict_Copy(PyObject *o) |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 2528 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2529 | PyObject *copy; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2530 | PyDictObject *mp; |
| 2531 | Py_ssize_t i, n; |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 2532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2533 | if (o == NULL || !PyDict_Check(o)) { |
| 2534 | PyErr_BadInternalCall(); |
| 2535 | return NULL; |
| 2536 | } |
Yury Selivanov | b0a7a03 | 2018-01-22 11:54:41 -0500 | [diff] [blame] | 2537 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2538 | mp = (PyDictObject *)o; |
Yury Selivanov | b0a7a03 | 2018-01-22 11:54:41 -0500 | [diff] [blame] | 2539 | if (mp->ma_used == 0) { |
| 2540 | /* The dict is empty; just return a new dict. */ |
| 2541 | return PyDict_New(); |
| 2542 | } |
| 2543 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2544 | if (_PyDict_HasSplitTable(mp)) { |
| 2545 | PyDictObject *split_copy; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2546 | Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys)); |
| 2547 | PyObject **newvalues; |
| 2548 | newvalues = new_values(size); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2549 | if (newvalues == NULL) |
| 2550 | return PyErr_NoMemory(); |
| 2551 | split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type); |
| 2552 | if (split_copy == NULL) { |
| 2553 | free_values(newvalues); |
| 2554 | return NULL; |
| 2555 | } |
| 2556 | split_copy->ma_values = newvalues; |
| 2557 | split_copy->ma_keys = mp->ma_keys; |
| 2558 | split_copy->ma_used = mp->ma_used; |
INADA Naoki | d1c82c5 | 2018-04-03 11:43:53 +0900 | [diff] [blame] | 2559 | split_copy->ma_version_tag = DICT_NEXT_VERSION(); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2560 | DK_INCREF(mp->ma_keys); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2561 | for (i = 0, n = size; i < n; i++) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2562 | PyObject *value = mp->ma_values[i]; |
| 2563 | Py_XINCREF(value); |
| 2564 | split_copy->ma_values[i] = value; |
| 2565 | } |
Benjamin Peterson | 7ce67e4 | 2012-04-24 10:32:57 -0400 | [diff] [blame] | 2566 | if (_PyObject_GC_IS_TRACKED(mp)) |
| 2567 | _PyObject_GC_TRACK(split_copy); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2568 | return (PyObject *)split_copy; |
| 2569 | } |
Yury Selivanov | b0a7a03 | 2018-01-22 11:54:41 -0500 | [diff] [blame] | 2570 | |
| 2571 | if (PyDict_CheckExact(mp) && mp->ma_values == NULL && |
| 2572 | (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3)) |
| 2573 | { |
| 2574 | /* Use fast-copy if: |
| 2575 | |
| 2576 | (1) 'mp' is an instance of a subclassed dict; and |
| 2577 | |
| 2578 | (2) 'mp' is not a split-dict; and |
| 2579 | |
| 2580 | (3) if 'mp' is non-compact ('del' operation does not resize dicts), |
| 2581 | do fast-copy only if it has at most 1/3 non-used keys. |
| 2582 | |
Ville Skyttä | 61f82e0 | 2018-04-20 23:08:45 +0300 | [diff] [blame] | 2583 | The last condition (3) is important to guard against a pathological |
Yury Selivanov | b0a7a03 | 2018-01-22 11:54:41 -0500 | [diff] [blame] | 2584 | case when a large dict is almost emptied with multiple del/pop |
| 2585 | operations and copied after that. In cases like this, we defer to |
| 2586 | PyDict_Merge, which produces a compacted copy. |
| 2587 | */ |
| 2588 | return clone_combined_dict(mp); |
| 2589 | } |
| 2590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2591 | copy = PyDict_New(); |
| 2592 | if (copy == NULL) |
| 2593 | return NULL; |
| 2594 | if (PyDict_Merge(copy, o, 1) == 0) |
| 2595 | return copy; |
| 2596 | Py_DECREF(copy); |
| 2597 | return NULL; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2600 | Py_ssize_t |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2601 | PyDict_Size(PyObject *mp) |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 2602 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2603 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2604 | PyErr_BadInternalCall(); |
| 2605 | return -1; |
| 2606 | } |
| 2607 | return ((PyDictObject *)mp)->ma_used; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2610 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2611 | PyDict_Keys(PyObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2612 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2613 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2614 | PyErr_BadInternalCall(); |
| 2615 | return NULL; |
| 2616 | } |
| 2617 | return dict_keys((PyDictObject *)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2620 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2621 | PyDict_Values(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2622 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2623 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2624 | PyErr_BadInternalCall(); |
| 2625 | return NULL; |
| 2626 | } |
| 2627 | return dict_values((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2630 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2631 | PyDict_Items(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2633 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2634 | PyErr_BadInternalCall(); |
| 2635 | return NULL; |
| 2636 | } |
| 2637 | return dict_items((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2640 | /* Return 1 if dicts equal, 0 if not, -1 if error. |
| 2641 | * Gets out as soon as any difference is detected. |
| 2642 | * Uses only Py_EQ comparison. |
| 2643 | */ |
| 2644 | static int |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2645 | dict_equal(PyDictObject *a, PyDictObject *b) |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | Py_ssize_t i; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2649 | if (a->ma_used != b->ma_used) |
| 2650 | /* can't be equal if # of entries differ */ |
| 2651 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2652 | /* Same # of entries -- check all of 'em. Exit early on any diff. */ |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2653 | for (i = 0; i < a->ma_keys->dk_nentries; i++) { |
| 2654 | PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i]; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2655 | PyObject *aval; |
| 2656 | if (a->ma_values) |
| 2657 | aval = a->ma_values[i]; |
| 2658 | else |
| 2659 | aval = ep->me_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2660 | if (aval != NULL) { |
| 2661 | int cmp; |
| 2662 | PyObject *bval; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2663 | PyObject *key = ep->me_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2664 | /* temporarily bump aval's refcount to ensure it stays |
| 2665 | alive until we're done with it */ |
| 2666 | Py_INCREF(aval); |
| 2667 | /* ditto for key */ |
| 2668 | Py_INCREF(key); |
Antoine Pitrou | 0e9958b | 2012-12-02 19:10:07 +0100 | [diff] [blame] | 2669 | /* reuse the known hash value */ |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 2670 | b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2671 | if (bval == NULL) { |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 2672 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2673 | Py_DECREF(aval); |
| 2674 | if (PyErr_Occurred()) |
| 2675 | return -1; |
| 2676 | return 0; |
| 2677 | } |
| 2678 | cmp = PyObject_RichCompareBool(aval, bval, Py_EQ); |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 2679 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2680 | Py_DECREF(aval); |
| 2681 | if (cmp <= 0) /* error or not equal */ |
| 2682 | return cmp; |
| 2683 | } |
| 2684 | } |
| 2685 | return 1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2686 | } |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2687 | |
| 2688 | static PyObject * |
| 2689 | dict_richcompare(PyObject *v, PyObject *w, int op) |
| 2690 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2691 | int cmp; |
| 2692 | PyObject *res; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | if (!PyDict_Check(v) || !PyDict_Check(w)) { |
| 2695 | res = Py_NotImplemented; |
| 2696 | } |
| 2697 | else if (op == Py_EQ || op == Py_NE) { |
| 2698 | cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w); |
| 2699 | if (cmp < 0) |
| 2700 | return NULL; |
| 2701 | res = (cmp == (op == Py_EQ)) ? Py_True : Py_False; |
| 2702 | } |
| 2703 | else |
| 2704 | res = Py_NotImplemented; |
| 2705 | Py_INCREF(res); |
| 2706 | return res; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2707 | } |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2708 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2709 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2710 | |
| 2711 | @coexist |
| 2712 | dict.__contains__ |
| 2713 | |
| 2714 | key: object |
| 2715 | / |
| 2716 | |
Serhiy Storchaka | 78d9e58 | 2017-01-25 00:30:04 +0200 | [diff] [blame] | 2717 | True if the dictionary has the specified key, else False. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2718 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2719 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2720 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 2721 | dict___contains__(PyDictObject *self, PyObject *key) |
Serhiy Storchaka | 19d2597 | 2017-02-04 08:05:07 +0200 | [diff] [blame] | 2722 | /*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2723 | { |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 2724 | register PyDictObject *mp = self; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2725 | Py_hash_t hash; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2726 | Py_ssize_t ix; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 2727 | PyObject *value; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2730 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2731 | hash = PyObject_Hash(key); |
| 2732 | if (hash == -1) |
| 2733 | return NULL; |
| 2734 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 2735 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2736 | if (ix == DKIX_ERROR) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2737 | return NULL; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 2738 | if (ix == DKIX_EMPTY || value == NULL) |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2739 | Py_RETURN_FALSE; |
| 2740 | Py_RETURN_TRUE; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Victor Stinner | 7dc6a5f | 2017-01-19 12:37:13 +0100 | [diff] [blame] | 2743 | /*[clinic input] |
| 2744 | dict.get |
| 2745 | |
| 2746 | key: object |
Serhiy Storchaka | 48088ee | 2017-01-19 19:00:30 +0200 | [diff] [blame] | 2747 | default: object = None |
Victor Stinner | 7dc6a5f | 2017-01-19 12:37:13 +0100 | [diff] [blame] | 2748 | / |
| 2749 | |
Serhiy Storchaka | 78d9e58 | 2017-01-25 00:30:04 +0200 | [diff] [blame] | 2750 | Return the value for key if key is in the dictionary, else default. |
Victor Stinner | 7dc6a5f | 2017-01-19 12:37:13 +0100 | [diff] [blame] | 2751 | [clinic start generated code]*/ |
| 2752 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2753 | static PyObject * |
Serhiy Storchaka | 48088ee | 2017-01-19 19:00:30 +0200 | [diff] [blame] | 2754 | dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value) |
Serhiy Storchaka | 78d9e58 | 2017-01-25 00:30:04 +0200 | [diff] [blame] | 2755 | /*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/ |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2756 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2757 | PyObject *val = NULL; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2758 | Py_hash_t hash; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2759 | Py_ssize_t ix; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2762 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | hash = PyObject_Hash(key); |
| 2764 | if (hash == -1) |
| 2765 | return NULL; |
| 2766 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 2767 | ix = (self->ma_keys->dk_lookup) (self, key, hash, &val); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2768 | if (ix == DKIX_ERROR) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2769 | return NULL; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 2770 | if (ix == DKIX_EMPTY || val == NULL) { |
Serhiy Storchaka | 48088ee | 2017-01-19 19:00:30 +0200 | [diff] [blame] | 2771 | val = default_value; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 2772 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | Py_INCREF(val); |
| 2774 | return val; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Benjamin Peterson | 00e9886 | 2013-03-07 22:16:29 -0500 | [diff] [blame] | 2777 | PyObject * |
| 2778 | PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj) |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2779 | { |
Benjamin Peterson | 00e9886 | 2013-03-07 22:16:29 -0500 | [diff] [blame] | 2780 | PyDictObject *mp = (PyDictObject *)d; |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2781 | PyObject *value; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2782 | Py_hash_t hash; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2783 | |
Benjamin Peterson | 00e9886 | 2013-03-07 22:16:29 -0500 | [diff] [blame] | 2784 | if (!PyDict_Check(d)) { |
| 2785 | PyErr_BadInternalCall(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | return NULL; |
Benjamin Peterson | 00e9886 | 2013-03-07 22:16:29 -0500 | [diff] [blame] | 2787 | } |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2789 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2790 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2791 | hash = PyObject_Hash(key); |
| 2792 | if (hash == -1) |
| 2793 | return NULL; |
| 2794 | } |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2795 | |
| 2796 | if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) { |
| 2797 | if (insertion_resize(mp) < 0) |
| 2798 | return NULL; |
| 2799 | } |
| 2800 | |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 2801 | Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2802 | if (ix == DKIX_ERROR) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2803 | return NULL; |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2804 | |
| 2805 | if (_PyDict_HasSplitTable(mp) && |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 2806 | ((ix >= 0 && value == NULL && mp->ma_used != ix) || |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2807 | (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) { |
| 2808 | if (insertion_resize(mp) < 0) { |
| 2809 | return NULL; |
| 2810 | } |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2811 | ix = DKIX_EMPTY; |
| 2812 | } |
| 2813 | |
| 2814 | if (ix == DKIX_EMPTY) { |
| 2815 | PyDictKeyEntry *ep, *ep0; |
| 2816 | value = defaultobj; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2817 | if (mp->ma_keys->dk_usable <= 0) { |
Victor Stinner | 3c336c5 | 2016-09-12 14:17:40 +0200 | [diff] [blame] | 2818 | if (insertion_resize(mp) < 0) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2819 | return NULL; |
Victor Stinner | 3c336c5 | 2016-09-12 14:17:40 +0200 | [diff] [blame] | 2820 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2821 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 2822 | Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash); |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2823 | ep0 = DK_ENTRIES(mp->ma_keys); |
| 2824 | ep = &ep0[mp->ma_keys->dk_nentries]; |
| 2825 | dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries); |
Benjamin Peterson | b1efa53 | 2013-03-04 09:47:50 -0500 | [diff] [blame] | 2826 | Py_INCREF(key); |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2827 | Py_INCREF(value); |
| 2828 | MAINTAIN_TRACKING(mp, key, value); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2829 | ep->me_key = key; |
| 2830 | ep->me_hash = hash; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 2831 | if (_PyDict_HasSplitTable(mp)) { |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2832 | assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL); |
| 2833 | mp->ma_values[mp->ma_keys->dk_nentries] = value; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2834 | } |
| 2835 | else { |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2836 | ep->me_value = value; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2837 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2838 | mp->ma_used++; |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 2839 | mp->ma_version_tag = DICT_NEXT_VERSION(); |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2840 | mp->ma_keys->dk_usable--; |
| 2841 | mp->ma_keys->dk_nentries++; |
| 2842 | assert(mp->ma_keys->dk_usable >= 0); |
| 2843 | } |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 2844 | else if (value == NULL) { |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2845 | value = defaultobj; |
| 2846 | assert(_PyDict_HasSplitTable(mp)); |
| 2847 | assert(ix == mp->ma_used); |
| 2848 | Py_INCREF(value); |
| 2849 | MAINTAIN_TRACKING(mp, key, value); |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 2850 | mp->ma_values[ix] = value; |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2851 | mp->ma_used++; |
| 2852 | mp->ma_version_tag = DICT_NEXT_VERSION(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2853 | } |
INADA Naoki | 93f26f7 | 2016-11-02 18:45:16 +0900 | [diff] [blame] | 2854 | |
| 2855 | assert(_PyDict_CheckConsistency(mp)); |
| 2856 | return value; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
Victor Stinner | 7dc6a5f | 2017-01-19 12:37:13 +0100 | [diff] [blame] | 2859 | /*[clinic input] |
| 2860 | dict.setdefault |
| 2861 | |
| 2862 | key: object |
Serhiy Storchaka | 48088ee | 2017-01-19 19:00:30 +0200 | [diff] [blame] | 2863 | default: object = None |
Victor Stinner | 7dc6a5f | 2017-01-19 12:37:13 +0100 | [diff] [blame] | 2864 | / |
| 2865 | |
Serhiy Storchaka | 78d9e58 | 2017-01-25 00:30:04 +0200 | [diff] [blame] | 2866 | Insert key with a value of default if key is not in the dictionary. |
| 2867 | |
| 2868 | Return the value for key if key is in the dictionary, else default. |
Victor Stinner | 7dc6a5f | 2017-01-19 12:37:13 +0100 | [diff] [blame] | 2869 | [clinic start generated code]*/ |
| 2870 | |
Benjamin Peterson | 00e9886 | 2013-03-07 22:16:29 -0500 | [diff] [blame] | 2871 | static PyObject * |
Serhiy Storchaka | 48088ee | 2017-01-19 19:00:30 +0200 | [diff] [blame] | 2872 | dict_setdefault_impl(PyDictObject *self, PyObject *key, |
| 2873 | PyObject *default_value) |
Serhiy Storchaka | 78d9e58 | 2017-01-25 00:30:04 +0200 | [diff] [blame] | 2874 | /*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/ |
Benjamin Peterson | 00e9886 | 2013-03-07 22:16:29 -0500 | [diff] [blame] | 2875 | { |
Victor Stinner | 7dc6a5f | 2017-01-19 12:37:13 +0100 | [diff] [blame] | 2876 | PyObject *val; |
Benjamin Peterson | 00e9886 | 2013-03-07 22:16:29 -0500 | [diff] [blame] | 2877 | |
Serhiy Storchaka | 48088ee | 2017-01-19 19:00:30 +0200 | [diff] [blame] | 2878 | val = PyDict_SetDefault((PyObject *)self, key, default_value); |
Benjamin Peterson | 00e9886 | 2013-03-07 22:16:29 -0500 | [diff] [blame] | 2879 | Py_XINCREF(val); |
| 2880 | return val; |
| 2881 | } |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2882 | |
| 2883 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2884 | dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 2885 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2886 | PyDict_Clear((PyObject *)mp); |
| 2887 | Py_RETURN_NONE; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2890 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2891 | dict_pop(PyDictObject *mp, PyObject *args) |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2892 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | PyObject *key, *deflt = NULL; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt)) |
| 2896 | return NULL; |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 2897 | |
Yury Selivanov | 684ef2c | 2016-10-28 19:01:21 -0400 | [diff] [blame] | 2898 | return _PyDict_Pop((PyObject*)mp, key, deflt); |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
| 2901 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 2902 | dict_popitem(PyDictObject *mp, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2903 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2904 | Py_ssize_t i, j; |
| 2905 | PyDictKeyEntry *ep0, *ep; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2906 | PyObject *res; |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2907 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2908 | /* Allocate the result tuple before checking the size. Believe it |
| 2909 | * or not, this allocation could trigger a garbage collection which |
| 2910 | * could empty the dict, so if we checked the size first and that |
| 2911 | * happened, the result would be an infinite loop (searching for an |
| 2912 | * entry that no longer exists). Note that the usual popitem() |
| 2913 | * idiom is "while d: k, v = d.popitem()". so needing to throw the |
| 2914 | * tuple away if the dict *is* empty isn't a significant |
| 2915 | * inefficiency -- possible, but unlikely in practice. |
| 2916 | */ |
| 2917 | res = PyTuple_New(2); |
| 2918 | if (res == NULL) |
| 2919 | return NULL; |
| 2920 | if (mp->ma_used == 0) { |
| 2921 | Py_DECREF(res); |
| 2922 | PyErr_SetString(PyExc_KeyError, |
| 2923 | "popitem(): dictionary is empty"); |
| 2924 | return NULL; |
| 2925 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2926 | /* Convert split table to combined table */ |
| 2927 | if (mp->ma_keys->dk_lookup == lookdict_split) { |
| 2928 | if (dictresize(mp, DK_SIZE(mp->ma_keys))) { |
| 2929 | Py_DECREF(res); |
| 2930 | return NULL; |
| 2931 | } |
| 2932 | } |
| 2933 | ENSURE_ALLOWS_DELETIONS(mp); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2934 | |
| 2935 | /* Pop last item */ |
| 2936 | ep0 = DK_ENTRIES(mp->ma_keys); |
| 2937 | i = mp->ma_keys->dk_nentries - 1; |
| 2938 | while (i >= 0 && ep0[i].me_value == NULL) { |
| 2939 | i--; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2940 | } |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2941 | assert(i >= 0); |
| 2942 | |
| 2943 | ep = &ep0[i]; |
| 2944 | j = lookdict_index(mp->ma_keys, ep->me_hash, i); |
| 2945 | assert(j >= 0); |
| 2946 | assert(dk_get_index(mp->ma_keys, j) == i); |
| 2947 | dk_set_index(mp->ma_keys, j, DKIX_DUMMY); |
| 2948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | PyTuple_SET_ITEM(res, 0, ep->me_key); |
| 2950 | PyTuple_SET_ITEM(res, 1, ep->me_value); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2951 | ep->me_key = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | ep->me_value = NULL; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2953 | /* We can't dk_usable++ since there is DKIX_DUMMY in indices */ |
| 2954 | mp->ma_keys->dk_nentries = i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2955 | mp->ma_used--; |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 2956 | mp->ma_version_tag = DICT_NEXT_VERSION(); |
Victor Stinner | 611b0fa | 2016-09-14 15:02:01 +0200 | [diff] [blame] | 2957 | assert(_PyDict_CheckConsistency(mp)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2958 | return res; |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2961 | static int |
| 2962 | dict_traverse(PyObject *op, visitproc visit, void *arg) |
| 2963 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2964 | PyDictObject *mp = (PyDictObject *)op; |
Benjamin Peterson | 55f4452 | 2016-09-05 12:12:59 -0700 | [diff] [blame] | 2965 | PyDictKeysObject *keys = mp->ma_keys; |
Serhiy Storchaka | 46825d2 | 2016-09-26 21:29:34 +0300 | [diff] [blame] | 2966 | PyDictKeyEntry *entries = DK_ENTRIES(keys); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2967 | Py_ssize_t i, n = keys->dk_nentries; |
| 2968 | |
Benjamin Peterson | 55f4452 | 2016-09-05 12:12:59 -0700 | [diff] [blame] | 2969 | if (keys->dk_lookup == lookdict) { |
| 2970 | for (i = 0; i < n; i++) { |
| 2971 | if (entries[i].me_value != NULL) { |
| 2972 | Py_VISIT(entries[i].me_value); |
| 2973 | Py_VISIT(entries[i].me_key); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2974 | } |
| 2975 | } |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 2976 | } |
| 2977 | else { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2978 | if (mp->ma_values != NULL) { |
Benjamin Peterson | 55f4452 | 2016-09-05 12:12:59 -0700 | [diff] [blame] | 2979 | for (i = 0; i < n; i++) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2980 | Py_VISIT(mp->ma_values[i]); |
| 2981 | } |
| 2982 | } |
| 2983 | else { |
Benjamin Peterson | 55f4452 | 2016-09-05 12:12:59 -0700 | [diff] [blame] | 2984 | for (i = 0; i < n; i++) { |
| 2985 | Py_VISIT(entries[i].me_value); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2986 | } |
| 2987 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2988 | } |
| 2989 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | static int |
| 2993 | dict_tp_clear(PyObject *op) |
| 2994 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2995 | PyDict_Clear(op); |
| 2996 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2999 | static PyObject *dictiter_new(PyDictObject *, PyTypeObject *); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 3000 | |
Serhiy Storchaka | 0ce7a3a | 2015-12-22 08:16:18 +0200 | [diff] [blame] | 3001 | Py_ssize_t |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 3002 | _PyDict_SizeOf(PyDictObject *mp) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3003 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 3004 | Py_ssize_t size, usable, res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3005 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3006 | size = DK_SIZE(mp->ma_keys); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 3007 | usable = USABLE_FRACTION(size); |
| 3008 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 3009 | res = _PyObject_SIZE(Py_TYPE(mp)); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3010 | if (mp->ma_values) |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 3011 | res += usable * sizeof(PyObject*); |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 3012 | /* If the dictionary is split, the keys portion is accounted-for |
| 3013 | in the type object. */ |
| 3014 | if (mp->ma_keys->dk_refcnt == 1) |
Victor Stinner | 98ee9d5 | 2016-09-08 09:33:56 -0700 | [diff] [blame] | 3015 | res += (sizeof(PyDictKeysObject) |
Victor Stinner | 98ee9d5 | 2016-09-08 09:33:56 -0700 | [diff] [blame] | 3016 | + DK_IXSIZE(mp->ma_keys) * size |
| 3017 | + sizeof(PyDictKeyEntry) * usable); |
Serhiy Storchaka | 0ce7a3a | 2015-12-22 08:16:18 +0200 | [diff] [blame] | 3018 | return res; |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | Py_ssize_t |
| 3022 | _PyDict_KeysSize(PyDictKeysObject *keys) |
| 3023 | { |
Victor Stinner | 98ee9d5 | 2016-09-08 09:33:56 -0700 | [diff] [blame] | 3024 | return (sizeof(PyDictKeysObject) |
Victor Stinner | 98ee9d5 | 2016-09-08 09:33:56 -0700 | [diff] [blame] | 3025 | + DK_IXSIZE(keys) * DK_SIZE(keys) |
| 3026 | + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry)); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
doko@ubuntu.com | 17210f5 | 2016-01-14 14:04:59 +0100 | [diff] [blame] | 3029 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3030 | dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored)) |
Serhiy Storchaka | 0ce7a3a | 2015-12-22 08:16:18 +0200 | [diff] [blame] | 3031 | { |
| 3032 | return PyLong_FromSsize_t(_PyDict_SizeOf(mp)); |
| 3033 | } |
| 3034 | |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 3035 | PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]"); |
| 3036 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3037 | PyDoc_STRVAR(sizeof__doc__, |
| 3038 | "D.__sizeof__() -> size of D in memory, in bytes"); |
| 3039 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3040 | PyDoc_STRVAR(pop__doc__, |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 3041 | "D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\ |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 3042 | If key is not found, d is returned if given, otherwise KeyError is raised"); |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 3043 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3044 | PyDoc_STRVAR(popitem__doc__, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 3045 | "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 3046 | 2-tuple; but raise KeyError if D is empty."); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 3047 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3048 | PyDoc_STRVAR(update__doc__, |
Brett Cannon | f275416 | 2013-05-11 14:46:48 -0400 | [diff] [blame] | 3049 | "D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\ |
| 3050 | If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\ |
| 3051 | If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\ |
| 3052 | In either case, this is followed by: for k in F: D[k] = F[k]"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 3053 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3054 | PyDoc_STRVAR(clear__doc__, |
| 3055 | "D.clear() -> None. Remove all items from D."); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 3056 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3057 | PyDoc_STRVAR(copy__doc__, |
| 3058 | "D.copy() -> a shallow copy of D"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 3059 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3060 | /* Forward */ |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3061 | static PyObject *dictkeys_new(PyObject *, PyObject *); |
| 3062 | static PyObject *dictitems_new(PyObject *, PyObject *); |
| 3063 | static PyObject *dictvalues_new(PyObject *, PyObject *); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3064 | |
Guido van Rossum | 45c85d1 | 2007-07-27 16:31:40 +0000 | [diff] [blame] | 3065 | PyDoc_STRVAR(keys__doc__, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3066 | "D.keys() -> a set-like object providing a view on D's keys"); |
Guido van Rossum | 45c85d1 | 2007-07-27 16:31:40 +0000 | [diff] [blame] | 3067 | PyDoc_STRVAR(items__doc__, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3068 | "D.items() -> a set-like object providing a view on D's items"); |
Guido van Rossum | 45c85d1 | 2007-07-27 16:31:40 +0000 | [diff] [blame] | 3069 | PyDoc_STRVAR(values__doc__, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | "D.values() -> an object providing a view on D's values"); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3071 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3072 | static PyMethodDef mapp_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 3073 | DICT___CONTAINS___METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3074 | {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST, |
| 3075 | getitem__doc__}, |
Serhiy Storchaka | 0ce7a3a | 2015-12-22 08:16:18 +0200 | [diff] [blame] | 3076 | {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3077 | sizeof__doc__}, |
Victor Stinner | 7dc6a5f | 2017-01-19 12:37:13 +0100 | [diff] [blame] | 3078 | DICT_GET_METHODDEF |
| 3079 | DICT_SETDEFAULT_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3080 | {"pop", (PyCFunction)dict_pop, METH_VARARGS, |
| 3081 | pop__doc__}, |
| 3082 | {"popitem", (PyCFunction)dict_popitem, METH_NOARGS, |
| 3083 | popitem__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3084 | {"keys", dictkeys_new, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3085 | keys__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3086 | {"items", dictitems_new, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3087 | items__doc__}, |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3088 | {"values", dictvalues_new, METH_NOARGS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3089 | values__doc__}, |
| 3090 | {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS, |
| 3091 | update__doc__}, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 3092 | DICT_FROMKEYS_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3093 | {"clear", (PyCFunction)dict_clear, METH_NOARGS, |
| 3094 | clear__doc__}, |
| 3095 | {"copy", (PyCFunction)dict_copy, METH_NOARGS, |
| 3096 | copy__doc__}, |
| 3097 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 3098 | }; |
| 3099 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 3100 | /* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */ |
Raymond Hettinger | bc0f2ab | 2003-11-25 21:12:14 +0000 | [diff] [blame] | 3101 | int |
| 3102 | PyDict_Contains(PyObject *op, PyObject *key) |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 3103 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3104 | Py_hash_t hash; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 3105 | Py_ssize_t ix; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3106 | PyDictObject *mp = (PyDictObject *)op; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 3107 | PyObject *value; |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 3108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3110 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3111 | hash = PyObject_Hash(key); |
| 3112 | if (hash == -1) |
| 3113 | return -1; |
| 3114 | } |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 3115 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 3116 | if (ix == DKIX_ERROR) |
| 3117 | return -1; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 3118 | return (ix != DKIX_EMPTY && value != NULL); |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3121 | /* Internal version of PyDict_Contains used when the hash value is already known */ |
| 3122 | int |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 3123 | _PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3124 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3125 | PyDictObject *mp = (PyDictObject *)op; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 3126 | PyObject *value; |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 3127 | Py_ssize_t ix; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3128 | |
INADA Naoki | 778928b | 2017-08-03 23:45:15 +0900 | [diff] [blame] | 3129 | ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 3130 | if (ix == DKIX_ERROR) |
| 3131 | return -1; |
INADA Naoki | ba60977 | 2016-12-07 20:41:42 +0900 | [diff] [blame] | 3132 | return (ix != DKIX_EMPTY && value != NULL); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 3135 | /* Hack to implement "key in dict" */ |
| 3136 | static PySequenceMethods dict_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3137 | 0, /* sq_length */ |
| 3138 | 0, /* sq_concat */ |
| 3139 | 0, /* sq_repeat */ |
| 3140 | 0, /* sq_item */ |
| 3141 | 0, /* sq_slice */ |
| 3142 | 0, /* sq_ass_item */ |
| 3143 | 0, /* sq_ass_slice */ |
| 3144 | PyDict_Contains, /* sq_contains */ |
| 3145 | 0, /* sq_inplace_concat */ |
| 3146 | 0, /* sq_inplace_repeat */ |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 3147 | }; |
| 3148 | |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 3149 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3150 | dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | PyObject *self; |
Victor Stinner | a9f61a5 | 2013-07-16 22:17:26 +0200 | [diff] [blame] | 3153 | PyDictObject *d; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3155 | assert(type != NULL && type->tp_alloc != NULL); |
| 3156 | self = type->tp_alloc(type, 0); |
Victor Stinner | a9f61a5 | 2013-07-16 22:17:26 +0200 | [diff] [blame] | 3157 | if (self == NULL) |
| 3158 | return NULL; |
Victor Stinner | a9f61a5 | 2013-07-16 22:17:26 +0200 | [diff] [blame] | 3159 | d = (PyDictObject *)self; |
Victor Stinner | ac2a4fe | 2013-07-16 22:19:00 +0200 | [diff] [blame] | 3160 | |
Victor Stinner | a9f61a5 | 2013-07-16 22:17:26 +0200 | [diff] [blame] | 3161 | /* The object has been implicitly tracked by tp_alloc */ |
| 3162 | if (type == &PyDict_Type) |
| 3163 | _PyObject_GC_UNTRACK(d); |
Victor Stinner | ac2a4fe | 2013-07-16 22:19:00 +0200 | [diff] [blame] | 3164 | |
| 3165 | d->ma_used = 0; |
Victor Stinner | 3b6a6b4 | 2016-09-08 12:51:24 -0700 | [diff] [blame] | 3166 | d->ma_version_tag = DICT_NEXT_VERSION(); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 3167 | d->ma_keys = new_keys_object(PyDict_MINSIZE); |
Victor Stinner | ac2a4fe | 2013-07-16 22:19:00 +0200 | [diff] [blame] | 3168 | if (d->ma_keys == NULL) { |
| 3169 | Py_DECREF(self); |
| 3170 | return NULL; |
| 3171 | } |
Victor Stinner | 611b0fa | 2016-09-14 15:02:01 +0200 | [diff] [blame] | 3172 | assert(_PyDict_CheckConsistency(d)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3173 | return self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 3176 | static int |
| 3177 | dict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 3178 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3179 | return dict_update_common(self, args, kwds, "dict"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3182 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3183 | dict_iter(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 3184 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3185 | return dictiter_new(dict, &PyDictIterKey_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 3186 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3187 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3188 | PyDoc_STRVAR(dictionary_doc, |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 3189 | "dict() -> new empty dictionary\n" |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 3190 | "dict(mapping) -> new dictionary initialized from a mapping object's\n" |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 3191 | " (key, value) pairs\n" |
| 3192 | "dict(iterable) -> new dictionary initialized as if via:\n" |
Tim Peters | 4d85953 | 2001-10-27 18:27:48 +0000 | [diff] [blame] | 3193 | " d = {}\n" |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 3194 | " for k, v in iterable:\n" |
Just van Rossum | a797d81 | 2002-11-23 09:45:04 +0000 | [diff] [blame] | 3195 | " d[k] = v\n" |
| 3196 | "dict(**kwargs) -> new dictionary initialized with the name=value pairs\n" |
| 3197 | " in the keyword argument list. For example: dict(one=1, two=2)"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 3198 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3199 | PyTypeObject PyDict_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3200 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3201 | "dict", |
| 3202 | sizeof(PyDictObject), |
| 3203 | 0, |
| 3204 | (destructor)dict_dealloc, /* tp_dealloc */ |
| 3205 | 0, /* tp_print */ |
| 3206 | 0, /* tp_getattr */ |
| 3207 | 0, /* tp_setattr */ |
| 3208 | 0, /* tp_reserved */ |
| 3209 | (reprfunc)dict_repr, /* tp_repr */ |
| 3210 | 0, /* tp_as_number */ |
| 3211 | &dict_as_sequence, /* tp_as_sequence */ |
| 3212 | &dict_as_mapping, /* tp_as_mapping */ |
Georg Brandl | 00da4e0 | 2010-10-18 07:32:48 +0000 | [diff] [blame] | 3213 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | 0, /* tp_call */ |
| 3215 | 0, /* tp_str */ |
| 3216 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3217 | 0, /* tp_setattro */ |
| 3218 | 0, /* tp_as_buffer */ |
| 3219 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3220 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */ |
| 3221 | dictionary_doc, /* tp_doc */ |
| 3222 | dict_traverse, /* tp_traverse */ |
| 3223 | dict_tp_clear, /* tp_clear */ |
| 3224 | dict_richcompare, /* tp_richcompare */ |
| 3225 | 0, /* tp_weaklistoffset */ |
| 3226 | (getiterfunc)dict_iter, /* tp_iter */ |
| 3227 | 0, /* tp_iternext */ |
| 3228 | mapp_methods, /* tp_methods */ |
| 3229 | 0, /* tp_members */ |
| 3230 | 0, /* tp_getset */ |
| 3231 | 0, /* tp_base */ |
| 3232 | 0, /* tp_dict */ |
| 3233 | 0, /* tp_descr_get */ |
| 3234 | 0, /* tp_descr_set */ |
| 3235 | 0, /* tp_dictoffset */ |
| 3236 | dict_init, /* tp_init */ |
| 3237 | PyType_GenericAlloc, /* tp_alloc */ |
| 3238 | dict_new, /* tp_new */ |
| 3239 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 3240 | }; |
| 3241 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3242 | PyObject * |
| 3243 | _PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key) |
| 3244 | { |
| 3245 | PyObject *kv; |
| 3246 | kv = _PyUnicode_FromId(key); /* borrowed */ |
Victor Stinner | 5b3b100 | 2013-07-22 23:50:57 +0200 | [diff] [blame] | 3247 | if (kv == NULL) { |
| 3248 | PyErr_Clear(); |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3249 | return NULL; |
Victor Stinner | 5b3b100 | 2013-07-22 23:50:57 +0200 | [diff] [blame] | 3250 | } |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3251 | return PyDict_GetItem(dp, kv); |
| 3252 | } |
| 3253 | |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 3254 | /* For backward compatibility with old dictionary interface */ |
| 3255 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3256 | PyObject * |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 3257 | PyDict_GetItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 3258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3259 | PyObject *kv, *rv; |
| 3260 | kv = PyUnicode_FromString(key); |
Victor Stinner | fdcbab9 | 2013-07-16 22:16:05 +0200 | [diff] [blame] | 3261 | if (kv == NULL) { |
| 3262 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3263 | return NULL; |
Victor Stinner | fdcbab9 | 2013-07-16 22:16:05 +0200 | [diff] [blame] | 3264 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3265 | rv = PyDict_GetItem(v, kv); |
| 3266 | Py_DECREF(kv); |
| 3267 | return rv; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | int |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 3271 | _PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item) |
| 3272 | { |
| 3273 | PyObject *kv; |
| 3274 | kv = _PyUnicode_FromId(key); /* borrowed */ |
| 3275 | if (kv == NULL) |
| 3276 | return -1; |
| 3277 | return PyDict_SetItem(v, kv, item); |
| 3278 | } |
| 3279 | |
| 3280 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 3281 | PyDict_SetItemString(PyObject *v, const char *key, PyObject *item) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 3282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3283 | PyObject *kv; |
| 3284 | int err; |
| 3285 | kv = PyUnicode_FromString(key); |
| 3286 | if (kv == NULL) |
| 3287 | return -1; |
| 3288 | PyUnicode_InternInPlace(&kv); /* XXX Should we really? */ |
| 3289 | err = PyDict_SetItem(v, kv, item); |
| 3290 | Py_DECREF(kv); |
| 3291 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | int |
Victor Stinner | 5fd2e5a | 2013-11-06 18:58:22 +0100 | [diff] [blame] | 3295 | _PyDict_DelItemId(PyObject *v, _Py_Identifier *key) |
| 3296 | { |
| 3297 | PyObject *kv = _PyUnicode_FromId(key); /* borrowed */ |
| 3298 | if (kv == NULL) |
| 3299 | return -1; |
| 3300 | return PyDict_DelItem(v, kv); |
| 3301 | } |
| 3302 | |
| 3303 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 3304 | PyDict_DelItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 3305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | PyObject *kv; |
| 3307 | int err; |
| 3308 | kv = PyUnicode_FromString(key); |
| 3309 | if (kv == NULL) |
| 3310 | return -1; |
| 3311 | err = PyDict_DelItem(v, kv); |
| 3312 | Py_DECREF(kv); |
| 3313 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 3314 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3315 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3316 | /* Dictionary iterator types */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3317 | |
| 3318 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3319 | PyObject_HEAD |
| 3320 | PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */ |
| 3321 | Py_ssize_t di_used; |
| 3322 | Py_ssize_t di_pos; |
| 3323 | PyObject* di_result; /* reusable result tuple for iteritems */ |
| 3324 | Py_ssize_t len; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3325 | } dictiterobject; |
| 3326 | |
| 3327 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3328 | dictiter_new(PyDictObject *dict, PyTypeObject *itertype) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3329 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3330 | dictiterobject *di; |
| 3331 | di = PyObject_GC_New(dictiterobject, itertype); |
| 3332 | if (di == NULL) |
| 3333 | return NULL; |
| 3334 | Py_INCREF(dict); |
| 3335 | di->di_dict = dict; |
| 3336 | di->di_used = dict->ma_used; |
| 3337 | di->di_pos = 0; |
| 3338 | di->len = dict->ma_used; |
| 3339 | if (itertype == &PyDictIterItem_Type) { |
| 3340 | di->di_result = PyTuple_Pack(2, Py_None, Py_None); |
| 3341 | if (di->di_result == NULL) { |
| 3342 | Py_DECREF(di); |
| 3343 | return NULL; |
| 3344 | } |
| 3345 | } |
| 3346 | else |
| 3347 | di->di_result = NULL; |
| 3348 | _PyObject_GC_TRACK(di); |
| 3349 | return (PyObject *)di; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | static void |
| 3353 | dictiter_dealloc(dictiterobject *di) |
| 3354 | { |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 3355 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
| 3356 | _PyObject_GC_UNTRACK(di); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3357 | Py_XDECREF(di->di_dict); |
| 3358 | Py_XDECREF(di->di_result); |
| 3359 | PyObject_GC_Del(di); |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | static int |
| 3363 | dictiter_traverse(dictiterobject *di, visitproc visit, void *arg) |
| 3364 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3365 | Py_VISIT(di->di_dict); |
| 3366 | Py_VISIT(di->di_result); |
| 3367 | return 0; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3368 | } |
| 3369 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 3370 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3371 | dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored)) |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 3372 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3373 | Py_ssize_t len = 0; |
| 3374 | if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used) |
| 3375 | len = di->len; |
| 3376 | return PyLong_FromSize_t(len); |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 3377 | } |
| 3378 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3379 | PyDoc_STRVAR(length_hint_doc, |
| 3380 | "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 3381 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3382 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3383 | dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored)); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3384 | |
| 3385 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 3386 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 3387 | static PyMethodDef dictiter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3388 | {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, |
| 3389 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3390 | {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS, |
| 3391 | reduce_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3392 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 3393 | }; |
| 3394 | |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 3395 | static PyObject* |
| 3396 | dictiter_iternextkey(dictiterobject *di) |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 3397 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3398 | PyObject *key; |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3399 | Py_ssize_t i; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3400 | PyDictKeysObject *k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3401 | PyDictObject *d = di->di_dict; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 3402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3403 | if (d == NULL) |
| 3404 | return NULL; |
| 3405 | assert (PyDict_Check(d)); |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 3406 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3407 | if (di->di_used != d->ma_used) { |
| 3408 | PyErr_SetString(PyExc_RuntimeError, |
| 3409 | "dictionary changed size during iteration"); |
| 3410 | di->di_used = -1; /* Make this state sticky */ |
| 3411 | return NULL; |
| 3412 | } |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 3413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3414 | i = di->di_pos; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3415 | k = d->ma_keys; |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3416 | assert(i >= 0); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3417 | if (d->ma_values) { |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3418 | if (i >= d->ma_used) |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 3419 | goto fail; |
| 3420 | key = DK_ENTRIES(k)[i].me_key; |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3421 | assert(d->ma_values[i] != NULL); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3422 | } |
| 3423 | else { |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3424 | Py_ssize_t n = k->dk_nentries; |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 3425 | PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i]; |
| 3426 | while (i < n && entry_ptr->me_value == NULL) { |
| 3427 | entry_ptr++; |
| 3428 | i++; |
| 3429 | } |
| 3430 | if (i >= n) |
| 3431 | goto fail; |
| 3432 | key = entry_ptr->me_key; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3433 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3434 | di->di_pos = i+1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3435 | di->len--; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3436 | Py_INCREF(key); |
| 3437 | return key; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3438 | |
| 3439 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3440 | di->di_dict = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 3441 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3442 | return NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3445 | PyTypeObject PyDictIterKey_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3446 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3447 | "dict_keyiterator", /* tp_name */ |
| 3448 | sizeof(dictiterobject), /* tp_basicsize */ |
| 3449 | 0, /* tp_itemsize */ |
| 3450 | /* methods */ |
| 3451 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 3452 | 0, /* tp_print */ |
| 3453 | 0, /* tp_getattr */ |
| 3454 | 0, /* tp_setattr */ |
| 3455 | 0, /* tp_reserved */ |
| 3456 | 0, /* tp_repr */ |
| 3457 | 0, /* tp_as_number */ |
| 3458 | 0, /* tp_as_sequence */ |
| 3459 | 0, /* tp_as_mapping */ |
| 3460 | 0, /* tp_hash */ |
| 3461 | 0, /* tp_call */ |
| 3462 | 0, /* tp_str */ |
| 3463 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3464 | 0, /* tp_setattro */ |
| 3465 | 0, /* tp_as_buffer */ |
| 3466 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3467 | 0, /* tp_doc */ |
| 3468 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 3469 | 0, /* tp_clear */ |
| 3470 | 0, /* tp_richcompare */ |
| 3471 | 0, /* tp_weaklistoffset */ |
| 3472 | PyObject_SelfIter, /* tp_iter */ |
| 3473 | (iternextfunc)dictiter_iternextkey, /* tp_iternext */ |
| 3474 | dictiter_methods, /* tp_methods */ |
| 3475 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3476 | }; |
| 3477 | |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 3478 | static PyObject * |
| 3479 | dictiter_iternextvalue(dictiterobject *di) |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3480 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3481 | PyObject *value; |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3482 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3483 | PyDictObject *d = di->di_dict; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3485 | if (d == NULL) |
| 3486 | return NULL; |
| 3487 | assert (PyDict_Check(d)); |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3489 | if (di->di_used != d->ma_used) { |
| 3490 | PyErr_SetString(PyExc_RuntimeError, |
| 3491 | "dictionary changed size during iteration"); |
| 3492 | di->di_used = -1; /* Make this state sticky */ |
| 3493 | return NULL; |
| 3494 | } |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3496 | i = di->di_pos; |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3497 | assert(i >= 0); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3498 | if (d->ma_values) { |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3499 | if (i >= d->ma_used) |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 3500 | goto fail; |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3501 | value = d->ma_values[i]; |
| 3502 | assert(value != NULL); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3503 | } |
| 3504 | else { |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3505 | Py_ssize_t n = d->ma_keys->dk_nentries; |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 3506 | PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i]; |
| 3507 | while (i < n && entry_ptr->me_value == NULL) { |
| 3508 | entry_ptr++; |
| 3509 | i++; |
| 3510 | } |
| 3511 | if (i >= n) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3512 | goto fail; |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 3513 | value = entry_ptr->me_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3514 | } |
| 3515 | di->di_pos = i+1; |
| 3516 | di->len--; |
| 3517 | Py_INCREF(value); |
| 3518 | return value; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3519 | |
| 3520 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3521 | di->di_dict = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 3522 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3523 | return NULL; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | PyTypeObject PyDictIterValue_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3527 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3528 | "dict_valueiterator", /* tp_name */ |
| 3529 | sizeof(dictiterobject), /* tp_basicsize */ |
| 3530 | 0, /* tp_itemsize */ |
| 3531 | /* methods */ |
| 3532 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 3533 | 0, /* tp_print */ |
| 3534 | 0, /* tp_getattr */ |
| 3535 | 0, /* tp_setattr */ |
| 3536 | 0, /* tp_reserved */ |
| 3537 | 0, /* tp_repr */ |
| 3538 | 0, /* tp_as_number */ |
| 3539 | 0, /* tp_as_sequence */ |
| 3540 | 0, /* tp_as_mapping */ |
| 3541 | 0, /* tp_hash */ |
| 3542 | 0, /* tp_call */ |
| 3543 | 0, /* tp_str */ |
| 3544 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3545 | 0, /* tp_setattro */ |
| 3546 | 0, /* tp_as_buffer */ |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 3547 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3548 | 0, /* tp_doc */ |
| 3549 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 3550 | 0, /* tp_clear */ |
| 3551 | 0, /* tp_richcompare */ |
| 3552 | 0, /* tp_weaklistoffset */ |
| 3553 | PyObject_SelfIter, /* tp_iter */ |
| 3554 | (iternextfunc)dictiter_iternextvalue, /* tp_iternext */ |
| 3555 | dictiter_methods, /* tp_methods */ |
| 3556 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3557 | }; |
| 3558 | |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 3559 | static PyObject * |
| 3560 | dictiter_iternextitem(dictiterobject *di) |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3561 | { |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 3562 | PyObject *key, *value, *result; |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3563 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3564 | PyDictObject *d = di->di_dict; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3566 | if (d == NULL) |
| 3567 | return NULL; |
| 3568 | assert (PyDict_Check(d)); |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3570 | if (di->di_used != d->ma_used) { |
| 3571 | PyErr_SetString(PyExc_RuntimeError, |
| 3572 | "dictionary changed size during iteration"); |
| 3573 | di->di_used = -1; /* Make this state sticky */ |
| 3574 | return NULL; |
| 3575 | } |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3577 | i = di->di_pos; |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3578 | assert(i >= 0); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3579 | if (d->ma_values) { |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3580 | if (i >= d->ma_used) |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 3581 | goto fail; |
| 3582 | key = DK_ENTRIES(d->ma_keys)[i].me_key; |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3583 | value = d->ma_values[i]; |
| 3584 | assert(value != NULL); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3585 | } |
| 3586 | else { |
INADA Naoki | ca2d8be | 2016-11-04 16:59:10 +0900 | [diff] [blame] | 3587 | Py_ssize_t n = d->ma_keys->dk_nentries; |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 3588 | PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i]; |
| 3589 | while (i < n && entry_ptr->me_value == NULL) { |
| 3590 | entry_ptr++; |
| 3591 | i++; |
| 3592 | } |
| 3593 | if (i >= n) |
| 3594 | goto fail; |
| 3595 | key = entry_ptr->me_key; |
| 3596 | value = entry_ptr->me_value; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3597 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3598 | di->di_pos = i+1; |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 3599 | di->len--; |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 3600 | Py_INCREF(key); |
| 3601 | Py_INCREF(value); |
| 3602 | result = di->di_result; |
| 3603 | if (Py_REFCNT(result) == 1) { |
| 3604 | PyObject *oldkey = PyTuple_GET_ITEM(result, 0); |
| 3605 | PyObject *oldvalue = PyTuple_GET_ITEM(result, 1); |
| 3606 | PyTuple_SET_ITEM(result, 0, key); /* steals reference */ |
| 3607 | PyTuple_SET_ITEM(result, 1, value); /* steals reference */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3608 | Py_INCREF(result); |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 3609 | Py_DECREF(oldkey); |
| 3610 | Py_DECREF(oldvalue); |
Serhiy Storchaka | 49f5cdd | 2016-10-09 23:08:05 +0300 | [diff] [blame] | 3611 | } |
| 3612 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3613 | result = PyTuple_New(2); |
| 3614 | if (result == NULL) |
| 3615 | return NULL; |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 3616 | PyTuple_SET_ITEM(result, 0, key); /* steals reference */ |
| 3617 | PyTuple_SET_ITEM(result, 1, value); /* steals reference */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3618 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3619 | return result; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3620 | |
| 3621 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3622 | di->di_dict = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 3623 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3624 | return NULL; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
| 3627 | PyTypeObject PyDictIterItem_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3628 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3629 | "dict_itemiterator", /* tp_name */ |
| 3630 | sizeof(dictiterobject), /* tp_basicsize */ |
| 3631 | 0, /* tp_itemsize */ |
| 3632 | /* methods */ |
| 3633 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 3634 | 0, /* tp_print */ |
| 3635 | 0, /* tp_getattr */ |
| 3636 | 0, /* tp_setattr */ |
| 3637 | 0, /* tp_reserved */ |
| 3638 | 0, /* tp_repr */ |
| 3639 | 0, /* tp_as_number */ |
| 3640 | 0, /* tp_as_sequence */ |
| 3641 | 0, /* tp_as_mapping */ |
| 3642 | 0, /* tp_hash */ |
| 3643 | 0, /* tp_call */ |
| 3644 | 0, /* tp_str */ |
| 3645 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3646 | 0, /* tp_setattro */ |
| 3647 | 0, /* tp_as_buffer */ |
| 3648 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3649 | 0, /* tp_doc */ |
| 3650 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 3651 | 0, /* tp_clear */ |
| 3652 | 0, /* tp_richcompare */ |
| 3653 | 0, /* tp_weaklistoffset */ |
| 3654 | PyObject_SelfIter, /* tp_iter */ |
| 3655 | (iternextfunc)dictiter_iternextitem, /* tp_iternext */ |
| 3656 | dictiter_methods, /* tp_methods */ |
| 3657 | 0, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3658 | }; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3659 | |
| 3660 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3661 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 3662 | dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored)) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3663 | { |
| 3664 | PyObject *list; |
| 3665 | dictiterobject tmp; |
| 3666 | |
| 3667 | list = PyList_New(0); |
| 3668 | if (!list) |
| 3669 | return NULL; |
| 3670 | |
| 3671 | /* copy the itertor state */ |
| 3672 | tmp = *di; |
| 3673 | Py_XINCREF(tmp.di_dict); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3674 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3675 | /* iterate the temporary into a list */ |
| 3676 | for(;;) { |
| 3677 | PyObject *element = 0; |
| 3678 | if (Py_TYPE(di) == &PyDictIterItem_Type) |
| 3679 | element = dictiter_iternextitem(&tmp); |
| 3680 | else if (Py_TYPE(di) == &PyDictIterKey_Type) |
| 3681 | element = dictiter_iternextkey(&tmp); |
| 3682 | else if (Py_TYPE(di) == &PyDictIterValue_Type) |
| 3683 | element = dictiter_iternextvalue(&tmp); |
| 3684 | else |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 3685 | Py_UNREACHABLE(); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3686 | if (element) { |
| 3687 | if (PyList_Append(list, element)) { |
| 3688 | Py_DECREF(element); |
| 3689 | Py_DECREF(list); |
| 3690 | Py_XDECREF(tmp.di_dict); |
| 3691 | return NULL; |
| 3692 | } |
| 3693 | Py_DECREF(element); |
| 3694 | } else |
| 3695 | break; |
| 3696 | } |
| 3697 | Py_XDECREF(tmp.di_dict); |
| 3698 | /* check for error */ |
| 3699 | if (tmp.di_dict != NULL) { |
| 3700 | /* we have an error */ |
| 3701 | Py_DECREF(list); |
| 3702 | return NULL; |
| 3703 | } |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 3704 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3707 | /***********************************************/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3708 | /* View objects for keys(), items(), values(). */ |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3709 | /***********************************************/ |
| 3710 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3711 | /* The instance lay-out is the same for all three; but the type differs. */ |
| 3712 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3713 | static void |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 3714 | dictview_dealloc(_PyDictViewObject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3715 | { |
INADA Naoki | a6296d3 | 2017-08-24 14:55:17 +0900 | [diff] [blame] | 3716 | /* bpo-31095: UnTrack is needed before calling any callbacks */ |
| 3717 | _PyObject_GC_UNTRACK(dv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3718 | Py_XDECREF(dv->dv_dict); |
| 3719 | PyObject_GC_Del(dv); |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | static int |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 3723 | dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg) |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 3724 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3725 | Py_VISIT(dv->dv_dict); |
| 3726 | return 0; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3729 | static Py_ssize_t |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 3730 | dictview_len(_PyDictViewObject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3731 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3732 | Py_ssize_t len = 0; |
| 3733 | if (dv->dv_dict != NULL) |
| 3734 | len = dv->dv_dict->ma_used; |
| 3735 | return len; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3736 | } |
| 3737 | |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 3738 | PyObject * |
| 3739 | _PyDictView_New(PyObject *dict, PyTypeObject *type) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3740 | { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 3741 | _PyDictViewObject *dv; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3742 | if (dict == NULL) { |
| 3743 | PyErr_BadInternalCall(); |
| 3744 | return NULL; |
| 3745 | } |
| 3746 | if (!PyDict_Check(dict)) { |
| 3747 | /* XXX Get rid of this restriction later */ |
| 3748 | PyErr_Format(PyExc_TypeError, |
| 3749 | "%s() requires a dict argument, not '%s'", |
| 3750 | type->tp_name, dict->ob_type->tp_name); |
| 3751 | return NULL; |
| 3752 | } |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 3753 | dv = PyObject_GC_New(_PyDictViewObject, type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3754 | if (dv == NULL) |
| 3755 | return NULL; |
| 3756 | Py_INCREF(dict); |
| 3757 | dv->dv_dict = (PyDictObject *)dict; |
| 3758 | _PyObject_GC_TRACK(dv); |
| 3759 | return (PyObject *)dv; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3760 | } |
| 3761 | |
Neal Norwitz | e36f2ba | 2007-02-26 23:12:28 +0000 | [diff] [blame] | 3762 | /* TODO(guido): The views objects are not complete: |
| 3763 | |
| 3764 | * support more set operations |
| 3765 | * support arbitrary mappings? |
| 3766 | - either these should be static or exported in dictobject.h |
| 3767 | - if public then they should probably be in builtins |
| 3768 | */ |
| 3769 | |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3770 | /* Return 1 if self is a subset of other, iterating over self; |
| 3771 | 0 if not; -1 if an error occurred. */ |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3772 | static int |
| 3773 | all_contained_in(PyObject *self, PyObject *other) |
| 3774 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3775 | PyObject *iter = PyObject_GetIter(self); |
| 3776 | int ok = 1; |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3778 | if (iter == NULL) |
| 3779 | return -1; |
| 3780 | for (;;) { |
| 3781 | PyObject *next = PyIter_Next(iter); |
| 3782 | if (next == NULL) { |
| 3783 | if (PyErr_Occurred()) |
| 3784 | ok = -1; |
| 3785 | break; |
| 3786 | } |
| 3787 | ok = PySequence_Contains(other, next); |
| 3788 | Py_DECREF(next); |
| 3789 | if (ok <= 0) |
| 3790 | break; |
| 3791 | } |
| 3792 | Py_DECREF(iter); |
| 3793 | return ok; |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | static PyObject * |
| 3797 | dictview_richcompare(PyObject *self, PyObject *other, int op) |
| 3798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3799 | Py_ssize_t len_self, len_other; |
| 3800 | int ok; |
| 3801 | PyObject *result; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3803 | assert(self != NULL); |
| 3804 | assert(PyDictViewSet_Check(self)); |
| 3805 | assert(other != NULL); |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3806 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3807 | if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) |
| 3808 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3810 | len_self = PyObject_Size(self); |
| 3811 | if (len_self < 0) |
| 3812 | return NULL; |
| 3813 | len_other = PyObject_Size(other); |
| 3814 | if (len_other < 0) |
| 3815 | return NULL; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3817 | ok = 0; |
| 3818 | switch(op) { |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3820 | case Py_NE: |
| 3821 | case Py_EQ: |
| 3822 | if (len_self == len_other) |
| 3823 | ok = all_contained_in(self, other); |
| 3824 | if (op == Py_NE && ok >= 0) |
| 3825 | ok = !ok; |
| 3826 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3828 | case Py_LT: |
| 3829 | if (len_self < len_other) |
| 3830 | ok = all_contained_in(self, other); |
| 3831 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3833 | case Py_LE: |
| 3834 | if (len_self <= len_other) |
| 3835 | ok = all_contained_in(self, other); |
| 3836 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3838 | case Py_GT: |
| 3839 | if (len_self > len_other) |
| 3840 | ok = all_contained_in(other, self); |
| 3841 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3843 | case Py_GE: |
| 3844 | if (len_self >= len_other) |
| 3845 | ok = all_contained_in(other, self); |
| 3846 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3848 | } |
| 3849 | if (ok < 0) |
| 3850 | return NULL; |
| 3851 | result = ok ? Py_True : Py_False; |
| 3852 | Py_INCREF(result); |
| 3853 | return result; |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 3856 | static PyObject * |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 3857 | dictview_repr(_PyDictViewObject *dv) |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 3858 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3859 | PyObject *seq; |
bennorth | d7773d9 | 2018-01-26 15:46:01 +0000 | [diff] [blame] | 3860 | PyObject *result = NULL; |
| 3861 | Py_ssize_t rc; |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 3862 | |
bennorth | d7773d9 | 2018-01-26 15:46:01 +0000 | [diff] [blame] | 3863 | rc = Py_ReprEnter((PyObject *)dv); |
| 3864 | if (rc != 0) { |
| 3865 | return rc > 0 ? PyUnicode_FromString("...") : NULL; |
| 3866 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3867 | seq = PySequence_List((PyObject *)dv); |
bennorth | d7773d9 | 2018-01-26 15:46:01 +0000 | [diff] [blame] | 3868 | if (seq == NULL) { |
| 3869 | goto Done; |
| 3870 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3871 | result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq); |
| 3872 | Py_DECREF(seq); |
bennorth | d7773d9 | 2018-01-26 15:46:01 +0000 | [diff] [blame] | 3873 | |
| 3874 | Done: |
| 3875 | Py_ReprLeave((PyObject *)dv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3876 | return result; |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3879 | /*** dict_keys ***/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3880 | |
| 3881 | static PyObject * |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 3882 | dictkeys_iter(_PyDictViewObject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3883 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3884 | if (dv->dv_dict == NULL) { |
| 3885 | Py_RETURN_NONE; |
| 3886 | } |
| 3887 | return dictiter_new(dv->dv_dict, &PyDictIterKey_Type); |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
| 3890 | static int |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 3891 | dictkeys_contains(_PyDictViewObject *dv, PyObject *obj) |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3892 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3893 | if (dv->dv_dict == NULL) |
| 3894 | return 0; |
| 3895 | return PyDict_Contains((PyObject *)dv->dv_dict, obj); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3898 | static PySequenceMethods dictkeys_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3899 | (lenfunc)dictview_len, /* sq_length */ |
| 3900 | 0, /* sq_concat */ |
| 3901 | 0, /* sq_repeat */ |
| 3902 | 0, /* sq_item */ |
| 3903 | 0, /* sq_slice */ |
| 3904 | 0, /* sq_ass_item */ |
| 3905 | 0, /* sq_ass_slice */ |
| 3906 | (objobjproc)dictkeys_contains, /* sq_contains */ |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3907 | }; |
| 3908 | |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3909 | static PyObject* |
| 3910 | dictviews_sub(PyObject* self, PyObject *other) |
| 3911 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3912 | PyObject *result = PySet_New(self); |
| 3913 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3914 | _Py_IDENTIFIER(difference_update); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3915 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3916 | if (result == NULL) |
| 3917 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3918 | |
Benjamin Peterson | f11b25b | 2016-03-03 22:05:36 -0800 | [diff] [blame] | 3919 | tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3920 | if (tmp == NULL) { |
| 3921 | Py_DECREF(result); |
| 3922 | return NULL; |
| 3923 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3925 | Py_DECREF(tmp); |
| 3926 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3929 | PyObject* |
| 3930 | _PyDictView_Intersect(PyObject* self, PyObject *other) |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3931 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3932 | PyObject *result = PySet_New(self); |
| 3933 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3934 | _Py_IDENTIFIER(intersection_update); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3936 | if (result == NULL) |
| 3937 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3938 | |
Benjamin Peterson | f11b25b | 2016-03-03 22:05:36 -0800 | [diff] [blame] | 3939 | tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3940 | if (tmp == NULL) { |
| 3941 | Py_DECREF(result); |
| 3942 | return NULL; |
| 3943 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3945 | Py_DECREF(tmp); |
| 3946 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | static PyObject* |
| 3950 | dictviews_or(PyObject* self, PyObject *other) |
| 3951 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3952 | PyObject *result = PySet_New(self); |
| 3953 | PyObject *tmp; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 3954 | _Py_IDENTIFIER(update); |
Victor Stinner | d1a9cc2 | 2011-10-13 22:51:17 +0200 | [diff] [blame] | 3955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3956 | if (result == NULL) |
| 3957 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3958 | |
Benjamin Peterson | f11b25b | 2016-03-03 22:05:36 -0800 | [diff] [blame] | 3959 | tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3960 | if (tmp == NULL) { |
| 3961 | Py_DECREF(result); |
| 3962 | return NULL; |
| 3963 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3965 | Py_DECREF(tmp); |
| 3966 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3967 | } |
| 3968 | |
| 3969 | static PyObject* |
| 3970 | dictviews_xor(PyObject* self, PyObject *other) |
| 3971 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3972 | PyObject *result = PySet_New(self); |
| 3973 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3974 | _Py_IDENTIFIER(symmetric_difference_update); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3976 | if (result == NULL) |
| 3977 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3978 | |
Benjamin Peterson | f11b25b | 2016-03-03 22:05:36 -0800 | [diff] [blame] | 3979 | tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3980 | if (tmp == NULL) { |
| 3981 | Py_DECREF(result); |
| 3982 | return NULL; |
| 3983 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3984 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3985 | Py_DECREF(tmp); |
| 3986 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
| 3989 | static PyNumberMethods dictviews_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3990 | 0, /*nb_add*/ |
| 3991 | (binaryfunc)dictviews_sub, /*nb_subtract*/ |
| 3992 | 0, /*nb_multiply*/ |
| 3993 | 0, /*nb_remainder*/ |
| 3994 | 0, /*nb_divmod*/ |
| 3995 | 0, /*nb_power*/ |
| 3996 | 0, /*nb_negative*/ |
| 3997 | 0, /*nb_positive*/ |
| 3998 | 0, /*nb_absolute*/ |
| 3999 | 0, /*nb_bool*/ |
| 4000 | 0, /*nb_invert*/ |
| 4001 | 0, /*nb_lshift*/ |
| 4002 | 0, /*nb_rshift*/ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4003 | (binaryfunc)_PyDictView_Intersect, /*nb_and*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4004 | (binaryfunc)dictviews_xor, /*nb_xor*/ |
| 4005 | (binaryfunc)dictviews_or, /*nb_or*/ |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 4006 | }; |
| 4007 | |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 4008 | static PyObject* |
| 4009 | dictviews_isdisjoint(PyObject *self, PyObject *other) |
| 4010 | { |
| 4011 | PyObject *it; |
| 4012 | PyObject *item = NULL; |
| 4013 | |
| 4014 | if (self == other) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 4015 | if (dictview_len((_PyDictViewObject *)self) == 0) |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 4016 | Py_RETURN_TRUE; |
| 4017 | else |
| 4018 | Py_RETURN_FALSE; |
| 4019 | } |
| 4020 | |
| 4021 | /* Iterate over the shorter object (only if other is a set, |
| 4022 | * because PySequence_Contains may be expensive otherwise): */ |
| 4023 | if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 4024 | Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self); |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 4025 | Py_ssize_t len_other = PyObject_Size(other); |
| 4026 | if (len_other == -1) |
| 4027 | return NULL; |
| 4028 | |
| 4029 | if ((len_other > len_self)) { |
| 4030 | PyObject *tmp = other; |
| 4031 | other = self; |
| 4032 | self = tmp; |
| 4033 | } |
| 4034 | } |
| 4035 | |
| 4036 | it = PyObject_GetIter(other); |
| 4037 | if (it == NULL) |
| 4038 | return NULL; |
| 4039 | |
| 4040 | while ((item = PyIter_Next(it)) != NULL) { |
| 4041 | int contains = PySequence_Contains(self, item); |
| 4042 | Py_DECREF(item); |
| 4043 | if (contains == -1) { |
| 4044 | Py_DECREF(it); |
| 4045 | return NULL; |
| 4046 | } |
| 4047 | |
| 4048 | if (contains) { |
| 4049 | Py_DECREF(it); |
| 4050 | Py_RETURN_FALSE; |
| 4051 | } |
| 4052 | } |
| 4053 | Py_DECREF(it); |
| 4054 | if (PyErr_Occurred()) |
| 4055 | return NULL; /* PyIter_Next raised an exception. */ |
| 4056 | Py_RETURN_TRUE; |
| 4057 | } |
| 4058 | |
| 4059 | PyDoc_STRVAR(isdisjoint_doc, |
| 4060 | "Return True if the view and the given iterable have a null intersection."); |
| 4061 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4062 | static PyMethodDef dictkeys_methods[] = { |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 4063 | {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O, |
| 4064 | isdisjoint_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4065 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4066 | }; |
| 4067 | |
| 4068 | PyTypeObject PyDictKeys_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4069 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 4070 | "dict_keys", /* tp_name */ |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 4071 | sizeof(_PyDictViewObject), /* tp_basicsize */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4072 | 0, /* tp_itemsize */ |
| 4073 | /* methods */ |
| 4074 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 4075 | 0, /* tp_print */ |
| 4076 | 0, /* tp_getattr */ |
| 4077 | 0, /* tp_setattr */ |
| 4078 | 0, /* tp_reserved */ |
| 4079 | (reprfunc)dictview_repr, /* tp_repr */ |
| 4080 | &dictviews_as_number, /* tp_as_number */ |
| 4081 | &dictkeys_as_sequence, /* tp_as_sequence */ |
| 4082 | 0, /* tp_as_mapping */ |
| 4083 | 0, /* tp_hash */ |
| 4084 | 0, /* tp_call */ |
| 4085 | 0, /* tp_str */ |
| 4086 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4087 | 0, /* tp_setattro */ |
| 4088 | 0, /* tp_as_buffer */ |
| 4089 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 4090 | 0, /* tp_doc */ |
| 4091 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 4092 | 0, /* tp_clear */ |
| 4093 | dictview_richcompare, /* tp_richcompare */ |
| 4094 | 0, /* tp_weaklistoffset */ |
| 4095 | (getiterfunc)dictkeys_iter, /* tp_iter */ |
| 4096 | 0, /* tp_iternext */ |
| 4097 | dictkeys_methods, /* tp_methods */ |
| 4098 | 0, |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4099 | }; |
| 4100 | |
| 4101 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4102 | dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4103 | { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 4104 | return _PyDictView_New(dict, &PyDictKeys_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 4107 | /*** dict_items ***/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4108 | |
| 4109 | static PyObject * |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 4110 | dictitems_iter(_PyDictViewObject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4112 | if (dv->dv_dict == NULL) { |
| 4113 | Py_RETURN_NONE; |
| 4114 | } |
| 4115 | return dictiter_new(dv->dv_dict, &PyDictIterItem_Type); |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | static int |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 4119 | dictitems_contains(_PyDictViewObject *dv, PyObject *obj) |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 4120 | { |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 4121 | int result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4122 | PyObject *key, *value, *found; |
| 4123 | if (dv->dv_dict == NULL) |
| 4124 | return 0; |
| 4125 | if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2) |
| 4126 | return 0; |
| 4127 | key = PyTuple_GET_ITEM(obj, 0); |
| 4128 | value = PyTuple_GET_ITEM(obj, 1); |
Raymond Hettinger | 6692f01 | 2016-09-18 21:46:08 -0700 | [diff] [blame] | 4129 | found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4130 | if (found == NULL) { |
| 4131 | if (PyErr_Occurred()) |
| 4132 | return -1; |
| 4133 | return 0; |
| 4134 | } |
Serhiy Storchaka | 753bca3 | 2017-05-20 12:30:02 +0300 | [diff] [blame] | 4135 | Py_INCREF(found); |
| 4136 | result = PyObject_RichCompareBool(value, found, Py_EQ); |
| 4137 | Py_DECREF(found); |
| 4138 | return result; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 4141 | static PySequenceMethods dictitems_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4142 | (lenfunc)dictview_len, /* sq_length */ |
| 4143 | 0, /* sq_concat */ |
| 4144 | 0, /* sq_repeat */ |
| 4145 | 0, /* sq_item */ |
| 4146 | 0, /* sq_slice */ |
| 4147 | 0, /* sq_ass_item */ |
| 4148 | 0, /* sq_ass_slice */ |
| 4149 | (objobjproc)dictitems_contains, /* sq_contains */ |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 4150 | }; |
| 4151 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4152 | static PyMethodDef dictitems_methods[] = { |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 4153 | {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O, |
| 4154 | isdisjoint_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4155 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4156 | }; |
| 4157 | |
| 4158 | PyTypeObject PyDictItems_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4159 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 4160 | "dict_items", /* tp_name */ |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 4161 | sizeof(_PyDictViewObject), /* tp_basicsize */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4162 | 0, /* tp_itemsize */ |
| 4163 | /* methods */ |
| 4164 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 4165 | 0, /* tp_print */ |
| 4166 | 0, /* tp_getattr */ |
| 4167 | 0, /* tp_setattr */ |
| 4168 | 0, /* tp_reserved */ |
| 4169 | (reprfunc)dictview_repr, /* tp_repr */ |
| 4170 | &dictviews_as_number, /* tp_as_number */ |
| 4171 | &dictitems_as_sequence, /* tp_as_sequence */ |
| 4172 | 0, /* tp_as_mapping */ |
| 4173 | 0, /* tp_hash */ |
| 4174 | 0, /* tp_call */ |
| 4175 | 0, /* tp_str */ |
| 4176 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4177 | 0, /* tp_setattro */ |
| 4178 | 0, /* tp_as_buffer */ |
| 4179 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 4180 | 0, /* tp_doc */ |
| 4181 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 4182 | 0, /* tp_clear */ |
| 4183 | dictview_richcompare, /* tp_richcompare */ |
| 4184 | 0, /* tp_weaklistoffset */ |
| 4185 | (getiterfunc)dictitems_iter, /* tp_iter */ |
| 4186 | 0, /* tp_iternext */ |
| 4187 | dictitems_methods, /* tp_methods */ |
| 4188 | 0, |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4189 | }; |
| 4190 | |
| 4191 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4192 | dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4193 | { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 4194 | return _PyDictView_New(dict, &PyDictItems_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 4197 | /*** dict_values ***/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4198 | |
| 4199 | static PyObject * |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 4200 | dictvalues_iter(_PyDictViewObject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4201 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | if (dv->dv_dict == NULL) { |
| 4203 | Py_RETURN_NONE; |
| 4204 | } |
| 4205 | return dictiter_new(dv->dv_dict, &PyDictIterValue_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4206 | } |
| 4207 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 4208 | static PySequenceMethods dictvalues_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4209 | (lenfunc)dictview_len, /* sq_length */ |
| 4210 | 0, /* sq_concat */ |
| 4211 | 0, /* sq_repeat */ |
| 4212 | 0, /* sq_item */ |
| 4213 | 0, /* sq_slice */ |
| 4214 | 0, /* sq_ass_item */ |
| 4215 | 0, /* sq_ass_slice */ |
| 4216 | (objobjproc)0, /* sq_contains */ |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 4217 | }; |
| 4218 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4219 | static PyMethodDef dictvalues_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4220 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4221 | }; |
| 4222 | |
| 4223 | PyTypeObject PyDictValues_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4224 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 4225 | "dict_values", /* tp_name */ |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 4226 | sizeof(_PyDictViewObject), /* tp_basicsize */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4227 | 0, /* tp_itemsize */ |
| 4228 | /* methods */ |
| 4229 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 4230 | 0, /* tp_print */ |
| 4231 | 0, /* tp_getattr */ |
| 4232 | 0, /* tp_setattr */ |
| 4233 | 0, /* tp_reserved */ |
| 4234 | (reprfunc)dictview_repr, /* tp_repr */ |
| 4235 | 0, /* tp_as_number */ |
| 4236 | &dictvalues_as_sequence, /* tp_as_sequence */ |
| 4237 | 0, /* tp_as_mapping */ |
| 4238 | 0, /* tp_hash */ |
| 4239 | 0, /* tp_call */ |
| 4240 | 0, /* tp_str */ |
| 4241 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4242 | 0, /* tp_setattro */ |
| 4243 | 0, /* tp_as_buffer */ |
| 4244 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 4245 | 0, /* tp_doc */ |
| 4246 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 4247 | 0, /* tp_clear */ |
| 4248 | 0, /* tp_richcompare */ |
| 4249 | 0, /* tp_weaklistoffset */ |
| 4250 | (getiterfunc)dictvalues_iter, /* tp_iter */ |
| 4251 | 0, /* tp_iternext */ |
| 4252 | dictvalues_methods, /* tp_methods */ |
| 4253 | 0, |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4254 | }; |
| 4255 | |
| 4256 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 4257 | dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored)) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4258 | { |
Eric Snow | 96c6af9 | 2015-05-29 22:21:39 -0600 | [diff] [blame] | 4259 | return _PyDictView_New(dict, &PyDictValues_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 4260 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 4261 | |
| 4262 | /* Returns NULL if cannot allocate a new PyDictKeysObject, |
| 4263 | but does not set an error */ |
| 4264 | PyDictKeysObject * |
| 4265 | _PyDict_NewKeysForClass(void) |
| 4266 | { |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 4267 | PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 4268 | if (keys == NULL) |
| 4269 | PyErr_Clear(); |
| 4270 | else |
| 4271 | keys->dk_lookup = lookdict_split; |
| 4272 | return keys; |
| 4273 | } |
| 4274 | |
| 4275 | #define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys) |
| 4276 | |
| 4277 | PyObject * |
| 4278 | PyObject_GenericGetDict(PyObject *obj, void *context) |
| 4279 | { |
| 4280 | PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj); |
| 4281 | if (dictptr == NULL) { |
| 4282 | PyErr_SetString(PyExc_AttributeError, |
| 4283 | "This object has no __dict__"); |
| 4284 | return NULL; |
| 4285 | } |
| 4286 | dict = *dictptr; |
| 4287 | if (dict == NULL) { |
| 4288 | PyTypeObject *tp = Py_TYPE(obj); |
| 4289 | if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) { |
| 4290 | DK_INCREF(CACHED_KEYS(tp)); |
| 4291 | *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp)); |
| 4292 | } |
| 4293 | else { |
| 4294 | *dictptr = dict = PyDict_New(); |
| 4295 | } |
| 4296 | } |
| 4297 | Py_XINCREF(dict); |
| 4298 | return dict; |
| 4299 | } |
| 4300 | |
| 4301 | int |
| 4302 | _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr, |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 4303 | PyObject *key, PyObject *value) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 4304 | { |
| 4305 | PyObject *dict; |
| 4306 | int res; |
| 4307 | PyDictKeysObject *cached; |
| 4308 | |
| 4309 | assert(dictptr != NULL); |
| 4310 | if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) { |
| 4311 | assert(dictptr != NULL); |
| 4312 | dict = *dictptr; |
| 4313 | if (dict == NULL) { |
| 4314 | DK_INCREF(cached); |
| 4315 | dict = new_dict_with_shared_keys(cached); |
| 4316 | if (dict == NULL) |
| 4317 | return -1; |
| 4318 | *dictptr = dict; |
| 4319 | } |
| 4320 | if (value == NULL) { |
| 4321 | res = PyDict_DelItem(dict, key); |
INADA Naoki | 2294f3a | 2017-02-12 13:51:30 +0900 | [diff] [blame] | 4322 | // Since key sharing dict doesn't allow deletion, PyDict_DelItem() |
| 4323 | // always converts dict to combined form. |
| 4324 | if ((cached = CACHED_KEYS(tp)) != NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 4325 | CACHED_KEYS(tp) = NULL; |
| 4326 | DK_DECREF(cached); |
| 4327 | } |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 4328 | } |
| 4329 | else { |
INADA Naoki | 2294f3a | 2017-02-12 13:51:30 +0900 | [diff] [blame] | 4330 | int was_shared = (cached == ((PyDictObject *)dict)->ma_keys); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 4331 | res = PyDict_SetItem(dict, key, value); |
INADA Naoki | 2294f3a | 2017-02-12 13:51:30 +0900 | [diff] [blame] | 4332 | if (was_shared && |
| 4333 | (cached = CACHED_KEYS(tp)) != NULL && |
| 4334 | cached != ((PyDictObject *)dict)->ma_keys) { |
Victor Stinner | 3d3f264 | 2016-12-15 17:21:23 +0100 | [diff] [blame] | 4335 | /* PyDict_SetItem() may call dictresize and convert split table |
| 4336 | * into combined table. In such case, convert it to split |
| 4337 | * table again and update type's shared key only when this is |
| 4338 | * the only dict sharing key with the type. |
| 4339 | * |
| 4340 | * This is to allow using shared key in class like this: |
| 4341 | * |
| 4342 | * class C: |
| 4343 | * def __init__(self): |
| 4344 | * # one dict resize happens |
| 4345 | * self.a, self.b, self.c = 1, 2, 3 |
| 4346 | * self.d, self.e, self.f = 4, 5, 6 |
| 4347 | * a = C() |
| 4348 | */ |
Benjamin Peterson | 15ee821 | 2012-04-24 14:44:18 -0400 | [diff] [blame] | 4349 | if (cached->dk_refcnt == 1) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 4350 | CACHED_KEYS(tp) = make_keys_shared(dict); |
Victor Stinner | 742da04 | 2016-09-07 17:40:12 -0700 | [diff] [blame] | 4351 | } |
| 4352 | else { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 4353 | CACHED_KEYS(tp) = NULL; |
| 4354 | } |
| 4355 | DK_DECREF(cached); |
Benjamin Peterson | 15ee821 | 2012-04-24 14:44:18 -0400 | [diff] [blame] | 4356 | if (CACHED_KEYS(tp) == NULL && PyErr_Occurred()) |
| 4357 | return -1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 4358 | } |
| 4359 | } |
| 4360 | } else { |
| 4361 | dict = *dictptr; |
| 4362 | if (dict == NULL) { |
| 4363 | dict = PyDict_New(); |
| 4364 | if (dict == NULL) |
| 4365 | return -1; |
| 4366 | *dictptr = dict; |
| 4367 | } |
| 4368 | if (value == NULL) { |
| 4369 | res = PyDict_DelItem(dict, key); |
| 4370 | } else { |
| 4371 | res = PyDict_SetItem(dict, key, value); |
| 4372 | } |
| 4373 | } |
| 4374 | return res; |
| 4375 | } |
| 4376 | |
| 4377 | void |
| 4378 | _PyDictKeys_DecRef(PyDictKeysObject *keys) |
| 4379 | { |
| 4380 | DK_DECREF(keys); |
| 4381 | } |