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