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